关于swift:SKNode在移除前执行动画

SKNode perform animation before removing

我正在制作一个 2d 横向卷轴。

有从右边产生的火箭。

当我向一个人投掷弹丸时,我希望它:

  • 假装它被击中了,也许在它的矢量路径上稍微向后移动,闪烁红色,诸如此类
  • 弹丸应该像箭头一样粘在火箭上(不幸的是,node.position = RocketNode.position 对我不起作用:|)
  • 如果敌人HP为0,则执行死亡动画
  • 死亡动画结束后,移除火箭和弹丸

下面是我一直试图开始工作的代码,我对哪个节点是哪个节点以及如何定位两个不同的节点感到困惑...... bodyA 和 bodyB 执行同样的事情也非常低效...

这一切都在 didBeginContact 函数中......我留下了一些注释代码,所有的疣。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 case projectileGroup | rocketgroup:
        if contact.bodyA.categoryBitMask == rocketgroup || contact.bodyB.categoryBitMask == rocketgroup
        {



            //projectileObject.physicsBody?.velocity = CGVectorMake(0 , 0)

            if Model.sharedInstance.sound == true

            {
                runAction(RocketExplosionPreload)
            }

            Model.sharedInstance.totalscore = Model.sharedInstance.totalscore + Model.sharedInstance.score


            RocketExplodeTexturesArray = [SKTexture(imageNamed:"RocketExplode0"),SKTexture(imageNamed:"RocketExplode1"),SKTexture(imageNamed:"RocketExplode2"),SKTexture(imageNamed:"RocketExplode3"),SKTexture(imageNamed:"RocketExplode4"),SKTexture(imageNamed:"RocketExplode5"),SKTexture(imageNamed:"RocketExplode6")]

            let RocketExplodeAnimation = SKAction.animateWithTextures(RocketExplodeTexturesArray, timePerFrame: 0.1)

            let RocketExp = SKAction.repeatAction(RocketExplodeAnimation, count: 1)

            print(contact.bodyA.node)
            print(contact.bodyB.node)



            if let node = contact.bodyA.node as? SKSpriteNode {
                print(node)
                //node.paused = true
                if node.parent != nil {


                    let moveUp = SKAction.moveToY(0, duration: 1)

                    let fadeOut = RocketExp



                    let remove = SKAction.removeFromParent()

                    let moveAndFade = SKAction.group([moveUp, fadeOut])

                    let sequence = SKAction.sequence([moveUp,fadeOut])




                    RocketNode?.runAction(sequence) {
                        /// remove theSprite from it's parent
                        /// Might need to weakly reference self here
                        RocketNode!.removeFromParent()
                        ProjectileNode!.removeFromParent()
                        print("Deleted")
                    }






                    //node.removeFromParent()
                }
            }
            if let node = contact.bodyB.node as? SKSpriteNode {
                print(node)
                //node.paused = true
                if node.parent != nil {

                    let moveUp = SKAction.moveToY(0, duration: 1)

                    let fadeOut = RocketExp



                    let remove = SKAction.removeFromParent()

                    let moveAndFade = SKAction.group([moveUp, fadeOut])

                    let sequence = SKAction.sequence([moveUp,fadeOut])




                    RocketNode?.runAction(sequence) {
                        /// remove theSprite from it's parent
                        /// Might need to weakly reference self here
                        RocketNode!.removeFromParent()
                        ProjectileNode!.removeFromParent()
                        print("Deleted")
                    }
                }
            }
        }


就像@whirlwind 评论的那样,您在这里同时提出了很多问题。
我还没有运行此代码,但它肯定会为您指明正确的方向。

1
2
3
4
5
6
7
8
class RocketNode: SKSpriteNode
{
  var HP: Int = 10
  func runHitAnimation()
  {
    // flash red or something
  }
}

检测联系人:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let projectileCategory: UInt32 = 4 // some power of 2
let rocketCategory: UInt32 = 8

func didBeginContact(contact: SKPhysicsContact)
{
  let firstBody = contact.bodyA
  let secondBody = contact.bodyB

  if (firstBody.categoryBitMask | secondBody.categoryBitMask) ==
    (projectileCategory | rocketCategory)
  {
    self.evaluateRocketProjectileContact(between: firstBody, and: secondBody)
  }
}

评估联系人:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
func evaluateRocketProjectileContact(between firstBody: SKPhysicsBody, and secondBody: SKPhysicsBody)
{
  var rocketBody: SKPhysicsBody
  var projectileBody: SKPhysicsBody

  if (firstBody.categoryBitMask & projectileCategory) > 0
  {
    projectileBody = firstBody
    rocketBody = secondBody
  }
  else
  {
    projectileBody = secondBody
    rocketBody = firstBody
  }

  let rocket = rocketBody.node as! RocketNode
  let projectile = projectileBody.node as! SKSpriteNode

  rocket.HP -= 1

  // move projectile to rocket and remove its physics body
  projectile.moveToParent(rocket)
  projectile.physicsBody = nil

  if rocket.HP == 0
  {
    // do something explosive animation
    let explode = SKAction.scaleBy(2.0, duration: 1.0)
    rocket.runAction(explode, completion: {
      rocket.removeFromParent()
    })
  }
}