【问题标题】:How can I get the real 'self', which node runs the SKaction Sequence如何获得真正的“自我”,哪个节点运行 SKaction 序列
【发布时间】:2020-04-26 15:29:51
【问题描述】:

我尝试设置像SKAction.fireFromEnemy() 这样的扩展名,并附加到动作序列,然后reuse 序列。我的麻烦是我无法获得真正的self,所以我必须在每次创建敌人时设置序列。

就像SKAction.removeFromParent(),哪个func可以针对节点运行动作。

【问题讨论】:

    标签: ios swift sprite-kit self skaction


    【解决方案1】:

    SKAction.customAction 可能就是你想要的。

    https://developer.apple.com/documentation/spritekit/skaction/1417745-customaction

    如果您希望它发生一次,请将持续时间设置为 0。

    SKAction.customAction(withDuration: 0) { node, time in node.fireFromEnemy() }

    【讨论】:

      【解决方案2】:

      Real self 作为 customAction 的第一个参数关闭

      例子:

      class TestClass {
          init() {
              let node = SKSpriteNode(color:.red,size:CGSize(width:100,height:100))
              let sequence = SKAction.sequence([.fireFromEnemy, .wait(forDuration: 10), .removeFromParent()])
              node.run(sequence)
          }
      }
      
      extension SKAction {
          static let fireFromEnemy = SKAction.customAction(withDuration: 0.3) { node, elapsedTime in
              // Do stuff here like:
              node.alpha = 0.5
              node.zRotation = .pi
          }
      }
      
      

      【讨论】:

        猜你喜欢
        • 2021-10-21
        • 1970-01-01
        • 2017-12-26
        • 1970-01-01
        • 2022-07-31
        • 2014-06-29
        • 1970-01-01
        • 2023-03-14
        • 2019-01-18
        相关资源
        最近更新 更多