2016-05-09 9 views
2

Ich bekomme einen sehr seltsamen Absturz beim Versuch, die Position eines SKSpriteNode zu ändern. Dieser Absturz passiert in iOS8 und nicht in iOS9.EXC_BAD_ACESS Absturz in SpriteKit auf iOS8

// 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() 

} 

Der Absturz passiert, wenn ich versuche, die Position zu setzen (fewerColorsPowerupIcon.position) und das ist der Crash-Nachricht:

Gewinde 1: EXC_BAD_ACCESS (code = EXC_I386_GPFLT)

Dieser Absturz passiert immer noch, wenn ich das .removeFromParent() Stück Code, nachdem ich die Position des Knotens gesetzt habe.

+0

haben Sie gerade versucht, das Objekt zu Null Einstellung stattdessen entfernen von Eltern ? –

+0

Wie wird wenigerColorsPowerupIcon deklariert? –

+0

@LouFranco das Objekt wird deklariert var lessColorsPowerupIcon: SKSpriteNode! – Onglo

Antwort

1

Ich denke, wenn Sie das Sprite entfernen, können Sie nicht tunithing.

so versuchen, dies zu tun:

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) 

oder Sie können zuerst in der Szene und nach dem Wechsel Eigenschaft hinzufügen:

// 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

Wenn fewerColorsPowerupIcon etwas erklärt wurden wie:

weak var fewerColorsPowerupIcon: Type! 

Dann, sobald Sie removeFromParent() nennen, könnte es ihm nicht mehr keine starken Referenzen sein. Wenn dies der Fall ist, ist nicht garantiert, dass fewerColorsPowerupIcon auf Null gesetzt wird oder dass es erkannt wird. Wenn Sie das nächste Mal verwenden, erhalten Sie möglicherweise EXC_BAD_ACCESS.

Eine Möglichkeit, so etwas zu finden, ist die Verwendung des Zombie-Instruments. Darunter werden keine Objekte wirklich freigegeben, wenn sie keine Referenzen haben, aber sie beschweren sich, wenn Sie versuchen, sie zu verwenden, sobald alle starken Referenzen freigegeben sind.

Eine andere Möglichkeit ist das Ändern der! zu? (und den Code aktualisieren, der die Variable verwendet). Es könnte ein Schmerz sein, aber es gibt mehr Garantien wie? handelt.