2016-05-09 2 views
2

Я получаю очень странный сбой при попытке изменить положение SKSpriteNode. Этот сбой происходит в iOS8, а не в iOS9.Ошибка EXC_BAD_ACESS в SpriteKit на 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() 

} 

Катастрофа происходит, когда я пытаюсь установить позицию (fewerColorsPowerupIcon.position), и это сообщение аварии:

Тема 1: EXC_BAD_ACCESS (код = EXC_I386_GPFLT)

Это аварии все еще случается, если я положил код .removeFromParent() после того, как я установил положение узла.

+0

вы пробовали просто установив объект на ноль вместо удаления из родителей ? –

+0

Как объявляется меньшее значениеColorsPowerupIcon? –

+0

@LouFranco объект объявлен var lesserColorsPowerupIcon: 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 были объявлены что-то вроде:

weak var fewerColorsPowerupIcon: Type! 

Тогда как только вы звоните removeFromParent(), не может быть никаких сильных ссылок на него больше. Если бы это было так, вам не гарантировано, что fewerColorsPowerupIcon будет установлен в нуль или что он будет обнаружен. В следующий раз, когда вы его используете, вы можете получить EXC_BAD_ACCESS.

Один из способов найти что-то вроде этого - использовать инструмент зомби. Под ним никакие объекты не освобождаются, если у них нет ссылок, но они жалуются, если вы попытаетесь использовать их, как только будут выпущены все сильные ссылки.

Другой вариант - изменить! к? (и обновить код, который использует var). Это может быть боль, но есть больше гарантий о том, как? действует.

Смежные вопросы