[英]'CGFloat' is not Convertible to 'Float' AND MORE
我的應用程序中有更多問題。 我的錯誤如標題所示:
錯誤1:'CGFloat'無法轉換為'Float'
第1和第2錯誤的代碼:
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)
}
Error1:'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:'CGFloat'與'UInt8'不同
第1和第2錯誤的代碼(兩行都相同):
ground1.position.x -= movingSpeed
ground2.position.x -= movingSpeed
錯誤2:無法使用類型為'(@lvalue CGFloat,$ T9)的參數列表調用' - ='
第3和第4個錯誤的代碼(兩行都相同):
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
。 我懷疑你在你提供的片段中沒有聲明的值也是如此,例如space
或movingSpeed
。
要了解CGFloat
是什么,按住Command鍵點擊它 - 這將帶你進入CoreGraphics
API,在那里你可以讀到CGFloat
是一個“本機類型......它是32位架構上的Float和64位架構上的Double”。
另外,我傾向於使用?
而不是!
修改physicsBody
,如果physicsBody
為零,則后者會崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.