Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – SKAction runAction不执行完成块大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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

换句话说,当且仅当该节点在场景中时,才运行节点的动作.通过调用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不执行完成块所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。