1

我在以下设备上运行我的 spritekit 游戏:iPhone 5s、iPhone 4s、iPad air 和 iPad mini。该应用程序运行和除 4s 之外的所有设备。在 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) {
   ........ }

应用程序崩溃时出现的错误消息是:thread 1 exc_breakpoint (code=exc_arm_breakpoint, subcode = 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

当我注释掉它之前崩溃的任何行时,错误一直在移动。一旦我删除了背景音乐和我调用到我的 didMoveToView(view: SKView) 中添加或修改新的或现有的 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 上没有任何调试会话......有人有吗关于这里发生了什么的最微弱的想法?

4

1 回答 1

3

问题是我使用这条线将节点放置在随机位置。

 let durationOne = Int(arc4random()) % Int(rangeDur) + Int(minDur)

我想这条线的问题是它在 32 位设备(如 4s)中无法处理随机数的大小并导致崩溃。当我将该行替换为:

 let durationOne = UInt32(arc4random()) % UInt32(rangeDur) + UInt32(minDur)

问题停止,设备运行平稳。虽然我不知道为什么错误消息没有针对这一行并一直移动到不相关的行......无论如何我希望这可以帮助任何有类似问题的人!

于 2015-01-28T15:18:00.650 回答