A SKSpriteNode
is child of a SKNode
and is placed on an array of SKSpriteNode
for storage purposes.
This SKSpriteNode
is deleted using an animation. At the end of this animation a completion block is executed to perform some statements...
The deletion must occurred both in the SKSpriteNode
parent’s and in the array. Depending on the order of these 2 deletions the result is correct or not:
- if
SKSpriteNode
is deleted from 1/ the array then 2/ from theSKNode
parent’s, the completion block is executed. - if the inverse order, 1/
SKNode
parent’s then 2/ the array, the completion block is not executed.
Why this behavior?
for position in listOfPositions {
theSprite:SKSpriteNode = theGrid[position]
/// the SKSpriteNode referenced by theSprite :
/// - belongs to an array of SKSpriteNode: theGrid
/// - belongs to a SKNode: theGameLayer
///
/// In other words this SKSpriteNode is referenced twice
///
let theActions = SKAction.sequence([
/// Some actions here
/// ...
/// Remove theSprite from the Grid
/// - position is an instance of a structure of my own
/// - theGrid is accessed via a subscript
///
SKAction.runBlock({self.theGrid[position] = nil}),
/// remove theSprite from it's parent
SKAction.removeFromParent(),
])
theSprite.runAction(theActions,completion:{NSLog("Deleted")})
}
The completion message is displayed.
Now if removeFromParent
is placed before the remove from theGrid
action, as follow, the completion does not execute:
let theActions = SKAction.sequence([
/// Some actions here
/// ...
/// remove theSprite from it's parent
SKAction.removeFromParent(),
/// remove theSprite from the Grid
SKAction.runBlock({self.theGrid[position] = nil}),
])
self
toposition
is not relevant in the situation: it leads to a compilation error. – Dominique Vialposition
is correct. It seems to be more related on howremoveFromParent
manage the execution of the completion block. – Dominique Vial