SKSpriteNode
是SKNode
的孩子,并存放在SKSpriteNode
数组中以用于存储目的。
使用动画删除此SKSpriteNode
。在此动画结束时,执行完成块以执行某些语句...
必须在SKSpriteNode
父级和数组中进行删除。根据这两个删除的顺序,结果是否正确:
SKSpriteNode
,则从SKNode
父项删除{/>,则执行完成块。SKNode
父级则为2 /数组,则不执行完成块。为什么会出现这种情况?
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")})
}
显示完成消息。
现在,如果在removeFromParent
操作移除之前放置theGrid
,请执行以下操作:
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}),
])
答案 0 :(得分:11)
SKAction对象是由节点中的节点执行的操作 scene(SKScene)...当场景处理其节点时,与之相关的动作 评估节点。
换句话说,当且仅当该节点在场景中时,才运行节点的动作。通过调用removeFromParent
,您从场景中删除节点,永远不会调用runBlock
动作(因为节点不再在场景中),因此序列永远不会完成。由于序列没有完成,因此不会调用完成块。
出于安全考虑,我建议将removeFromParent
调用移至完成块。这样的事情感觉更安全:
for position in listOfPositions {
let 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})
])
theSprite.runAction(theActions) {
/// remove theSprite from it's parent
/// Might need to weakly reference self here
theSprite.removeFromParent(),
NSLog("Deleted")
}
}
<强> TL; DR 强>
序列没有完成,因此序列的完成块不会被调用。