加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – SKAction runAction不执行完成块

发布时间:2020-12-14 05:31:28 所属栏目:百科 来源:网络整理
导读:SKSpriteNode是SKNode的子节点,放置在SKSpriteNode数组中以用于存储目的. 使用动画删除此SKSpriteNode.在此动画结束时,执行完成块以执行某些语句… 删除必须同时发生在SKSpriteNode父级和数组中.根据这两个删除的顺序,结果是否正确: 如果从1 /数组中删除SKS
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序列没有完成,因此序列的完成块不会被调用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读