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

穿精又带淫゛_ 提交于 2019-12-07 11:26:22

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/25719472/ios-8-code-working-on-iphone-5s-but-not-iphone-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!