0

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

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Jeremy
  • 1
  • 1
    What type is heightBetweenObstacles – Christos Chadjikyriacou Apr 02 '15 at 09:47
  • Note that `CGPointMake` takes two `CGFloats` as arguments, not `Int`s. –  Apr 02 '15 at 09:51
  • It's a constant defined earlier in the code as 'let heightBetweenObstacles = 907' – Jeremy Apr 02 '15 at 09:53
  • `bottomtiki.position.y` will likely be a `CGFloat`, to which an `Int` is (attempted to be) added. –  Apr 02 '15 at 09:58
  • I'm sorry, but I'm new to coding. How would that look like written out, if you don't mind please? I've set this up earlier in the code 'bottomtiki.position = CGPointMake(0, CGFloat(yPos))' – Jeremy Apr 02 '15 at 10:00
  • `CGPointMake(0.0, bottomtiki.position.y + CGFloat(heightBetweenObstacles))` –  Apr 02 '15 at 10:02
  • Related (and possibly duplicate): http://stackoverflow.com/questions/24645798/does-swift-support-implicit-conversion . –  Apr 02 '15 at 10:06
  • Thank you Evert! I didn't even think to add that in. I appreciate the help :) – Jeremy Apr 02 '15 at 10:23

1 Answers1

0

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

andyvn22
  • 14,696
  • 1
  • 52
  • 74