Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 Code working on iPhone 5s but not iPhone 5

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

enter image description here

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

like image 629
MikeB Avatar asked Dec 26 '22 04:12

MikeB


1 Answers

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)
like image 144
onevcat Avatar answered Jan 15 '23 16:01

onevcat