简体   繁体   中英

'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.

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 '?' 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?

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:

let heightBetweenObstacles = 42

Swift will infer the constant's type, and in this case, it will infer Int . When you try to add it to a CGFloat ( bottomtiki.position.y + heightBetweenObstacles ), it complains. Just explicitly specify the type instead:

let heightBetweenObstacles: CGFloat = 42

Now you'll have no trouble adding the constant to other CGFloat s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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