2016-05-09 4 views
2

SKSpriteNodeの位置を変更しようとすると、非常に奇妙なクラッシュが発生します。このクラッシュはiOS8ではなくiOS9で発生します。iOS8のSpriteKitでEXC_BAD_ACESSがクラッシュする

// if the powerup is full, spawn the icon on screen 
if orangeBallsCollected == numberOfBallsRequiredToActivatePowerup { 

    // add the powerup icon to the array of powerupiconsonscreen 
    powerupIconsOnScreen.append(fewerColorsPowerupIcon) 

    // set the lighting mask of the powerup icon so that we know what positon it is onscreen for alter 
    fewerColorsPowerupIcon.lightingBitMask = UInt32(powerupIconsOnScreen.count - 1) 

    // remove the ball from its current parent 
    fewerColorsPowerupIcon.removeFromParent() 

    print(fewerColorsPowerupIcon, fewerColorsPowerupIcon.parent, fewerColorsPowerupIcon.physicsBody, fewerColorsPowerupIcon.superclass) 

    // place the ball of the screen so that we can bring it on later 
    fewerColorsPowerupIcon.position = CGPointMake((width * -0.1) , (height * -0.1)) 

    // set the size of the icon 
    fewerColorsPowerupIcon.xScale = scaleFactor 
    fewerColorsPowerupIcon.yScale = scaleFactor 

    // add it the scene 
    self.addChild(fewerColorsPowerupIcon) 

    // animate it moving down to the first avaliable position 
    let animation = SKAction.moveTo(CGPoint(x: width * 0.1, y: height * 0.1), duration: 0.5) 

    // run the animation! 
    fewerColorsPowerupIcon.runAction(animation) 

    // activate the poweurp 
    activateFewerColors() 

} 

I位置(fewerColorsPowerupIcon.position)を設定しようとすると、クラッシュが発生し、これがクラッシュメッセージである:

スレッド1:EXC_BAD_ACCESS(コード= EXC_I386_GPFLT)

このクラッシュノードの位置を設定した後に.removeFromParent()コードを置くと、やはりエラーが発生します。もはやそれへの強い参照がないかもしれないとすぐにremoveFromParent()を呼び出すとその後

weak var fewerColorsPowerupIcon: Type! 

、:fewerColorsPowerupIconのようなものを宣言していた場合

+0

はあなただけnilにオブジェクトを設定する代わりに、親から削除しようとしています? –

+0

lessColorsPowerupIconはどのように宣言されていますか? –

+0

@LouFrancoオブジェクトは宣言されていますvar lessColorsPowerupIcon:SKSpriteNode! – Onglo

答えて

1

スプライトを削除すると、アニメーションができないと思います。

ので、これを行うにしてみてください。

let spriteCopy = fewerColorsPowerupIcon.copy() 

// place the ball of the screen so that we can bring it on later 
spriteCopy.position = CGPointMake((width * -0.1) , (height * -0.1)) 

// set the size of the icon 
spriteCopy.xScale = scaleFactor 
spriteCopy.yScale = scaleFactor 

// add it the scene 
self.addChild(spriteCopy) 

またはあなたが最初のシーンで、変更プロパティの後に追加することができます。

// remove the ball from its current parent 
fewerColorsPowerupIcon.removeFromParent() 

// add it the scene 
self.addChild(fewerColorsPowerupIcon) 

// place the ball of the screen so that we can bring it on later 
fewerColorsPowerupIcon.position = CGPointMake((width * -0.1) , (height * -0.1)) 

// set the size of the icon 
fewerColorsPowerupIcon.xScale = scaleFactor 
fewerColorsPowerupIcon.yScale = scaleFactor 
0

。そうであれば、fewerColorsPowerupIconがnilに設定されているか、それが検出されることは保証されません。次回使用すると、EXC_BAD_ACCESSが表示されることがあります。

このようなものを見つける1つの方法は、ゾンビ器具を使用することです。その下には、参照を持たないオブジェクトは実際に解放されませんが、すべての強力な参照が解放されるとオブジェクトを使用しようとすると文句を言います。

もう一つのオプションは、! 〜に? (そしてvarを使用するコードを更新する)。それは痛みかもしれませんが、どのように保証されていますか?行為。

関連する問題