After testing my spritekit game on the iPhone 5s simulator all the time I finally tried to run it on the iPhone 5 simulator. Unfortunately I get an error as soon as I do the first touch that I don't understand. My touchesBegan function calls my addCoin function (see below)
The error is somewhere in this code-block. If I comment out this part of the code everything else works fine:
func addCoin()
{
var coin:SKSpriteNode = SKSpriteNode(texture: coinFrames[0])
coin.size.width = 50
coin.size.height = 50
coin.physicsBody = SKPhysicsBody(circleOfRadius: coin.size.height / 2)
coin.physicsBody?.dynamic = false
coin.physicsBody?.allowsRotation = false
coin.physicsBody?.categoryBitMask = coinCategory
coin.physicsBody?.contactTestBitMask = playerCategory
var positionX:CGFloat = CGFloat(Int(arc4random()) % Int(500)) + CGFloat(70.0)
var positionY:CGFloat = CGFloat(Int(arc4random()) % Int(1007)) + CGFloat(63.0)
coin.position = CGPointMake(positionX, positionY)
coin.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(coinFrames, timePerFrame: 0.1, resize:false, restore:true)))
self.addChild(coin)
}
Here is the error that occurs. If I comment out this line, the next one gives an error...
Like I said, iPhone 5s works perfectly... What could be wrong with my code?
xCode 6 beta 6 & iOS 8 beta 7
Thanks in advance
The debugger is misleading you. The real problem is arc4random
, which will return an UInt32
on both iPhone 5 and 5s. But as iPhone 5 is a 32-bit device, the Int(arc4random())
will cause an overflow if the random number is big enough.
Instead of using Int(arc4random())
, you can try to replace it by using arc4random_uniform
. Maybe the code below will do the trick for you.
var positionX: CGFloat = CGFloat(arc4random_uniform(500)) + CGFloat(70.0)
var positionY: CGFloat = CGFloat(arc4random_uniform(1007)) + CGFloat(63.0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With