【发布时间】:2015-06-02 14:34:01
【问题描述】:
SKSpriteNode 是 SKNode 的子代,并且出于存储目的放置在 SKSpriteNode 的数组中。
这个SKSpriteNode 使用动画删除。在这个动画的最后会执行一个完成块来执行一些语句......
删除必须同时发生在 SKSpriteNode 父级和数组中。根据这 2 次删除的顺序,结果是否正确:
- 如果
SKSpriteNode从数组 1/ 中删除,然后从SKNode父数组中删除 2/,则执行完成块。 - 如果逆序,1/
SKNodeparent 的 then 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 放在remove from 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}),
])
【问题讨论】:
-
试试这个:SKAction.runBlock({self.theGrid[self.position] = nil}),
-
将
self添加到position与这种情况无关:它会导致编译错误。 -
我建议写 self 因为我认为这就像一个范围问题。
-
您的建议是可以的,但不适用于这种情况,因为
position的范围是正确的。它似乎与removeFromParent如何管理完成块的执行更相关。 -
我已通过添加解释来更新问题以更好地理解上下文。
标签: swift sprite-kit