iOS 8代码可在iPhone 5s上运行,但不能在iPhone 5上运行 [英] iOS 8 Code working on iPhone 5s but not iPhone 5
问题描述
在所有时间都在iPhone 5s模拟器上测试了spritekit游戏之后,我终于尝试在iPhone 5模拟器上运行它.不幸的是,一旦我不了解我的第一触碰,我就会收到一个错误.我的touchesBegan函数调用了我的addCoin函数(见下文)
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...
就像我说的那样,iPhone 5s可以完美运行... 我的代码可能出什么问题了?
Like I said, iPhone 5s works perfectly... What could be wrong with my code?
xCode 6 beta 6& iOS 8 beta 7
xCode 6 beta 6 & iOS 8 beta 7
预先感谢
推荐答案
调试器会误导您.真正的问题是arc4random
,它将在iPhone 5和5s上都返回UInt32
.但是由于iPhone 5是32位设备,如果随机数足够大,则Int(arc4random())
会导致溢出.
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.
代替使用Int(arc4random())
,您可以尝试使用arc4random_uniform
替换它.也许下面的代码将为您解决问题.
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)
这篇关于iOS 8代码可在iPhone 5s上运行,但不能在iPhone 5上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!