如何使用Sprite Kit场景同时制作动画和移动播放器节点?

时间:2019-01-03 21:31:21

标签: swift sprite-kit

我正在开发一款小型的iOS游戏,并在其中实现了此joystick library。我的问题是计算操纵杆的方向后,我希望角色更改为正在运行的动画(我使用.sks文件实现动画)。它几乎可以正常工作,除了动画开始之后,它会停止并且直到播放器放开操纵杆才结束。我的一些代码在下面。任何帮助表示赞赏。

设置操纵杆的功能:

func setupJoystick() {
    addChild(analogJoyStick)

    analogJoyStick.trackingHandler = { [unowned self] data in
        self.thePlayer.position = CGPoint(x: self.thePlayer.position.x + (data.velocity.x * 0.04), y: self.thePlayer.position.y + (data.velocity.y * 0.04))

        let degrees = self.analogJoyStick.data.angular * 360 / (2 * .pi)
        if degrees > 0 {
         let walkAnimation:SKAction = SKAction(named: "WalkLeft")!
         self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
        } else if degrees < 0 {
         let walkAnimation:SKAction = SKAction(named: "WalkRight")!
         self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
        }
    }

    analogJoyStick.beginHandler = { [unowned self] in
        let degrees = self.analogJoyStick.data.angular * 360 / (2 * .pi)
        if degrees > 0 {
            let walkAnimation:SKAction = SKAction(named: "WalkLeft")!
            self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
        } else if degrees < 0 {
            let walkAnimation:SKAction = SKAction(named: "WalkRight")!
            self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
        }
    }

    analogJoyStick.stopHandler = { [unowned self] in
        self.thePlayer.removeAction(forKey: "animating")
    }
}

这是代码的视觉效果: Spritekit Demo

1 个答案:

答案 0 :(得分:0)

我阅读了joystick library的说明,并看到了可以使用的两种方法(处理程序):

DropDownList.SelectedItem.Value == "By Serial Number"

var beginHandler: (() -> Void)? // before move var stopHandler: (() -> Void)? // after move 中添加(重复)步行动画:

beginHandler()

并删除let walkAnimation: SKAction = SKAction(named: theAnimation)! thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")

中的操作
stopHandler()

或使用闭包(也来自文档):

thePlayer.removeAction(forKey: "animating")