SKNode在删除之前执行动画

时间:2016-07-24 19:22:53

标签: swift sprite-kit

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

有火箭从右边产生。

当我把一个抛射物扔到一个时,我希望它:

  • 表现得像是被击中了,或许在它的矢量路径上略微向后移动,闪红色,那种事情
  • 射弹应该像箭一样插入火箭(node.position = RocketNode.position对我不起作用,不幸的是:|)
  • 如果敌人HP为0,则执行死亡动画
  • 死亡动画后,移除火箭和射弹

下面是我一直在努力工作的代码,我很困惑哪个节点是哪个,以及如何定位两个不同的节点... bodyA和bodyB执行同样的事情也是非常低效的..

这一切都在didBeginContact函数中...我已经留下了一些注释代码,咋咋。

 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")
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

就像@whirlwind评论的那样,你在这里同时提出了很多问题。 我没有运行此代码,但它绝对应该指向正确的方向。

font=$1
text2image ... --font="$1" ...

检测联系人:

class RocketNode: SKSpriteNode
{
  var HP: Int = 10
  func runHitAnimation()
  {
    // flash red or something
  }
}

评估联系人:

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)
  }
}