swift – SKAction runAction不执行完成块

swift – SKAction runAction不执行完成块,第1张

概述SKSpriteNode是SKNode的子节点,放置在SKSpriteNode数组中以用于存储目的. 使用动画删除此SKSpriteNode.在此动画结束时,执行完成块以执行某些语句… 删除必须同时发生在SKSpriteNode父级和数组中.根据这两个删除的顺序,结果是否正确: >如果从1 /数组中删除SKSpriteNode然后从SKNode父项删除2 /,则执行完成块. >如果是逆序,1 / 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序列没有完成,因此序列的完成块不会被调用.

总结

以上是内存溢出为你收集整理的swift – SKAction runAction不执行完成块全部内容,希望文章能够帮你解决swift – SKAction runAction不执行完成块所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1035986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存