gpt4 book ai didi

ios - 'CGFloat' 不可转换为 'Float' 及更多

转载 作者:搜寻专家 更新时间:2023-10-30 21:57:58 24 4
gpt4 key购买 nike

我的应用中有更多问题。我的错误正如标题所说:

  1. Swift 编译器错误。

错误 1:“CGFloat”不能转换为“Float”

第一个和第二个错误的代码:

self.setPositionRelativeBot(pipeBot, x: xx, y: offset)
self.setPositionRelativeTop(pipeTop, x: xx, y: offset + space)

这是整个函数:

func spawnPipeRow(offs: Float)
{
let offset = offs - space / 2

let pipeBot = mainPipe.copy() as Pipe
let pipeTop = mainPipe.copy() as Pipe

pipeBot.texture = SKTexture(imageNamed: "BotPipe")
pipeTop.texture = SKTexture(imageNamed: "TopPipe")

pipeBot.texture!.filteringMode = SKTextureFilteringMode.Nearest
pipeTop.texture!.filteringMode = SKTextureFilteringMode.Nearest

pipeBot.isBottom = true

let xx = self.view!.bounds.size.width

self.setPositionRelativeBot(pipeBot, x: xx, y: offset)
self.setPositionRelativeTop(pipeTop, x: xx, y: offset + space)

pipeBot.physicsBody = SKPhysicsBody(rectangleOfSize: pipeBot.size)
pipeTop.physicsBody = SKPhysicsBody(rectangleOfSize: pipeTop.size)

pipeBot.physicsBody!.dynamic = false
pipeTop.physicsBody!.dynamic = false

pipeBot.physicsBody!.contactTestBitMask = birdCategory
pipeTop.physicsBody!.contactTestBitMask = birdCategory

pipeBot.physicsBody!.collisionBitMask = birdCategory
pipeTop.physicsBody!.collisionBitMask = birdCategory

pipes.append(pipeBot)
pipes.append(pipeTop)

self.addChild(pipeBot)
self.addChild(pipeTop)
}

  1. Swift 编译器错误。

错误 1:“Float”与“UInt8”不同

第一个错误的代码:

vel -= 85 - (self.view!.bounds.size.height - bird.position.y)

错误2:“SKPhysicsBody?”没有名为“velocity”的成员

第二个错误的代码:

bird.physicsBody.velocity = CGVectorMake(0, vel)

这是整个函数:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
if (!bird.physicsBody!.dynamic)
{
//First touch

self.spawnPipeRow(0)

bird.physicsBody!.dynamic = true

bird.physicsBody!.velocity = CGVectorMake(0, 175)

scoreLabel.hidden = false

isMoving = true
} else if (isMoving)
{
var vel: Float = 200

if (self.view!.bounds.size.height - bird.position.y < 85)
{
vel -= 85 - (self.view!.bounds.size.height - bird.position.y)
}

bird.physicsBody.velocity = CGVectorMake(0, vel)
} else
{
overlay.removeFromParent()

for pi in pipes
{
pi.removeFromParent()
}

pipes.removeAll(keepCapacity: false)

score = 0

bird.physicsBody!.dynamic = false
bird.position = CGPoint(x: 150, y: view!.bounds.size.height / 2 - 10)

scoreLabel.hidden = true

isGroundMoving = true
}
}

  1. Swift 编译器错误。

错误 1:“CGFloat”与“UInt8”不同

第一个和第二个错误的代码(两行相同的错误):

ground1.position.x -= movingSpeed
ground2.position.x -= movingSpeed

错误2:无法使用类型为“(@lvalue CGFloat, $T9)”的参数列表调用“-=”

第三个和第四个错误的代码(两行相同的错误):

background1.position.x -= movingSpeed / 3
background2.position.x -= movingSpeed / 3

错误3:“CGFloat”与“UInt8”不同

pipe.position.x -= movingSpeed

这是整个函数:

override func update(currentTime: CFTimeInterval)
{
if (isGroundMoving)
{
ground1.position.x -= movingSpeed
ground2.position.x -= movingSpeed

if (ground1.position.x <= -self.view!.bounds.size.width / 2)
{
ground1.position.x = self.view!.bounds.size.width * 1.5 - 2
}

if (ground2.position.x <= -self.view!.bounds.size.width / 2)
{
ground2.position.x = self.view!.bounds.size.width * 1.5 - 2
}

background1.position.x -= movingSpeed / 3
background2.position.x -= movingSpeed / 3

if (background1.position.x <= -self.view!.bounds.size.width / 2)
{
background1.position.x = self.view!.bounds.size.width * 1.5 - 2
}

if (background2.position.x <= -self.view!.bounds.size.width / 2)
{
background2.position.x = self.view!.bounds.size.width * 1.5 - 2
}

if (isMoving)
{
for (var i = 0; i < pipes.count; i++)
{
let pipe = pipes[i]

if (pipe.position.x + (pipe.size.width / 2) < 0)
{
pipe.removeFromParent()

continue
}

if (pipe.position.x + (pipe.size.width / 2) < self.view!.bounds.size.width / 2 && pipe.isBottom && !pipe.pointAdded)
{
score++

pipe.pointAdded = true
}

pipe.position.x -= movingSpeed

if (i == pipes.count - 1)
{
if (pipe.position.x < self.view!.bounds.width - pipe.size.width * 2.0)
{
self.spawnPipeRow(self.randomOffset())
}
}
}

scoreLabel.text = "Score: \(score)"
}
}
}

我希望你们中的一些人能帮我解决这个问题,因为我做不到:)

最佳答案

错误 1:使用 CGFloat 而不是 Float。例如:

var vel: CGFloat = 200

错误 2:physicsBody 是可选的(可以为 nil),因此在引用其 velocity 之前使用 ?. 运算符有条件地展开它:

bird.physicsBody?.velocity = CGVectorMake(0, vel)

您的所有错误似乎都是由这两个问题之一引起的。例如,spawnPipeRow 的参数再次是 offs: Float 而不是 CGFloat。我怀疑您未在您提供的代码段中声明的值也是如此,例如 spacemovingSpeed

要了解 CGFloat 是什么,按住 Command 单击它 – 这将带您到 CoreGraphics API,您可以在其中阅读 CGFloat 是“原生类型……在 32 位架构上是 Float,在 64 位架构上是 Double。”

此外,在修改 physicsBody 时,我倾向于使用 ? 而不是 !,因为如果 physicsBody 为零。

关于ios - 'CGFloat' 不可转换为 'Float' 及更多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26094881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com