我在以下设备上运行了我的spritekit游戏:iPhone 5s,iPhone 4s,iPad air和iPad mini。该应用程序运行和除4s以外的所有设备。在应用程序转换到包含SKPhysicsContactDelegate的场景时,应用程序崩溃,例如:
class PlayScene: SKScene, SKPhysicsContactDelegate, ADBannerViewDelegate {
enum ColliderType: UInt32 {
case narrowCategory = 1
case bigCategory = 2
case smallCategory = 4
case roundCategory = 8
}
var narrow = SKSpriteNode(imageNamed: "Narrow")
var big = SKSpriteNode(imageNamed: "Big")
override func didMoveToView(view: SKView) {
........ }
应用崩溃时出现的错误消息是: 主题1 exc_breakpoint(代码= exc_arm_breakpoint,子代码= 0xe7ffdefe) 。
最初错误停止了应用程序在这一行,我添加了背景音乐(这对我没有任何意义,因为这行也在其他场景之一,没有SKPhysicsContactDelegate,但它运行顺利):
var backgroundMusicURL: NSURL = NSBundle.mainBundle().URLForResource("LevelOne", withExtension: "wav")!
当我评论出背景音乐时,应用程序然后在此行崩溃
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("addPlay"), name: "AddPlay", object: nil)
在我再次评论之后,错误跳转到此行
narrow.physicsBody?.mass = 0.0001
然后这个
narrow.physicsBody?.allowsRotation = true
并且错误继续移动,因为我注释掉之前崩溃过的任何行。一旦我删除了背景音乐和我调用的所有函数,我添加或修改了新的或现有的spritenodes,我的应用程序停止崩溃,其中一个函数的示例如下:
func addDiamond() {
var diamond = SKSpriteNode(texture: diamondTexture)
diamond.name = "diamond"
diamond.physicsBody = SKPhysicsBody(circleOfRadius: diamond.size.width/2)
diamond.zPosition = 1
diamond.physicsBody?.mass = 0.01
diamond.physicsBody?.allowsRotation = true
diamond.physicsBody?.dynamic = true
diamond.physicsBody?.affectedByGravity = false
diamond.setScale(0.75)
diamond.physicsBody?.categoryBitMask = diamondCategory
diamond.physicsBody?.contactTestBitMask = laserCategory
diamond.physicsBody?.collisionBitMask = laserCategory
// setting the position
let minX = diamond.size.width/2
let maxX = self.frame.size.width - diamond.size.width/2
let rangeX = maxX - minX
let positionX: CGFloat = round(CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX))
let minY = self.frame.size.height/5
let maxY = self.frame.size.height * 0.4
let rangeY = maxY - minY
let positionY: CGFloat = round(CGFloat(arc4random()) % CGFloat(rangeY) + CGFloat(minY))
diamond.position = CGPointMake(positionX, positionY)
// setting the rotation
let minR = 0
let maxR = 6
let rangeR = maxR - minR
let positionR: CGFloat = round(CGFloat(arc4random()) % CGFloat(rangeR) + CGFloat(minR))
diamond.zRotation = positionR
// set animation
let hold = SKAction.waitForDuration(4)
let remove = SKAction.removeFromParent()
diamond.runAction(SKAction.sequence([hold,remove]))
// animation of bubble
let minDur = 6
let maxDur = 8
let rangeDur = maxDur - minDur
let durationOne = Int(arc4random()) % Int(rangeDur) + Int(minDur)
let durOne = NSTimeInterval((Double(durationOne)/25))
var animateArray:NSMutableArray = NSMutableArray()
for i in 1...1500 {
animateArray.addObject(SKAction.rotateByAngle(0.2, duration: 0.01))
//animateArray.addObject(SKAction.rotateByAngle(-0.2, duration: 0.5))
}
diamond.runAction(SKAction.sequence(animateArray))
animateArray.addObject(SKAction.removeFromParent())
addChild(diamond)
}
使用viewdidload
中的以下行添加 diamondTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: Selector("addDiamond"), userInfo: nil, repeats: true)
不使用SKPhysicsContactDelegate的场景在4s上运行正常。我还在模拟器上测试了4s,当你到达使用SKPhysicsContactDelegate的场景时它似乎崩溃了但是与设备不同的是没有出现错误,设备只是在xcode上没有任何调试会话时出现黑屏并崩溃...有没有人有关于这里发生了什么的最微弱的想法?
答案 0 :(得分:3)
问题在于我使用这条线将我的节点放在一个随机位置。
let durationOne = Int(arc4random()) % Int(rangeDur) + Int(minDur)
我认为这一行的问题在于它在32位设备(如4s)中设备无法处理随机数的大小而导致崩溃。当我用:
替换该行时 let durationOne = UInt32(arc4random()) % UInt32(rangeDur) + UInt32(minDur)
问题停止,设备运行平稳。虽然我不知道为什么错误信息没有针对这条线并继续转向不相关的线...无论如何,我希望这可以帮助任何有类似问题的人!