简体   繁体   English

'CGFloat不能转换为'int'

[英]'CGFloat is not convertible to 'int'

I've been wracking my mind the last several hours in trying to fix this single error in Xcode 6.2 using swift. 最近几个小时,我一直在努力尝试使用swift修复Xcode 6.2中的单个错误。

spriteName = "tiki-top-0\(rand)"
    var toptiki = SKSpriteNode(imageNamed: spriteName)

    //This line gives me the error
    toptiki.position = CGPointMake(0, bottomtiki.position.y + heightBetweenObstacles) 

    tikiSet.addChild(toptiki)
    toptiki.physicsBody = SKPhysicsBody(rectangleOfSize: toptiki.size)
    toptiki.physicsBody?.dynamic = false
    toptiki.physicsBody?.categoryBitMask = category_tiki
    toptiki.physicsBody?.contactTestBitMask = category_car

I've been searching everywhere and for some reason I really just think there's a syntax error somewhere. 我一直到处搜索,由于某种原因,我真的只是认为某个地方存在语法错误。 I've had the same issue with the physicsBody?.contactTestBitMask since the update to Xcode 6 and it's betas where as before the '?' 自从更新Xcode 6及其beta以来,我在PhysicalBody?.contactTestBitMask上也遇到了同样的问题。 was not necessary. 没必要。 But now for some reason it has to be chained. 但是现在由于某种原因,它必须被链接起来。

I'm not using a CGFloat here, why is it telling me I shouldn't be using an int? 我在这里没有使用CGFloat,为什么它告诉我不应该使用int?

Any help is greatly appreciated, I really can't fry my brain any further, lol 非常感谢您的帮助,我真的再也无法炒脑了,哈哈

I'm assuming heightBetweenObstacles is defined earlier along these lines: 我假设heightBetweenObstacles是按照以下heightBetweenObstacles行定义的:

let heightBetweenObstacles = 42

Swift will infer the constant's type, and in this case, it will infer Int . Swift将推断常量的类型,在这种情况下,它将推断Int When you try to add it to a CGFloat ( bottomtiki.position.y + heightBetweenObstacles ), it complains. 当您尝试将其添加到CGFloatbottomtiki.position.y + heightBetweenObstacles )时,它会抱怨。 Just explicitly specify the type instead: 只需显式指定类型即可:

let heightBetweenObstacles: CGFloat = 42

Now you'll have no trouble adding the constant to other CGFloat s. 现在,您可以毫不费力地将常量添加到其他CGFloat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM