SKSpriteNode是SKNode的子节点,放置在SKSpriteNode数组中以用于存储目的.
使用动画删除此SKSpriteNode.在此动画结束时,执行完成块以执行某些语句…
删除必须同时发生在SKSpriteNode父级和数组中.根据这两个删除的顺序,结果是否正确:
>如果从1 /数组中删除SKSpriteNode然后从SKNode父项删除2 /,则执行完成块.
>如果是逆序,1 / 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")}) }
将显示完成消息.
现在,如果在从theGrid操作中删除removeFromParent之前,如下所示,则不会执行完成:
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 Reference:
An SKAction object is an action that is executed by a node in the
scene (SKScene)…When the scene processes its nodes,actions associated with those
nodes are evaluated.
换句话说,当且仅当该节点在场景中时,才运行节点的动作.通过调用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序列没有完成,因此序列的完成块不会被调用.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。