【问题标题】:How to animate and move player node simultaneously, using sprite kit scene?如何使用精灵套件场景同时动画和移动玩家节点?
【发布时间】:2019-01-15 05:47:28
【问题描述】:

我正在开发一个带有joystick library 的小型ios 游戏。我的问题是在计算了操纵杆前进的方向之后,我希望角色变为正在运行的动画(我使用 .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

【问题讨论】:

  • 我之前已经尝试过这个问题的答案。结果是相同的,只是动画要么无限期播放,要么播放设定的时间。
  • 您是否尝试过在希望停止时删除旧动画?
  • 这是答案的一部分,但@P。 van der Laan 帮我找到了我想要的东西。感谢您的帮助。

标签: swift sprite-kit


【解决方案1】:

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

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

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

joystick.beginHandler = { [unowned self] in
  let walkAnimation: SKAction = SKAction(named: ".sks")!
  self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
}

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

【讨论】:

  • 顺便说一句,您是否知道我如何使用处理程序在多个动画之间进行切换?
  • 在跟踪处理程序中,速度和角度数据可用。根据愤怒,你可以开始不同的行动。只需确保操作键相同 - 最后更容易删除。
  • 我已经尝试在上面的代码中实现处理程序,但我无法让动画完全播放。当我向左或向右移动我的角色时,动画会在移动时停留在第一帧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 2015-12-10
  • 2020-05-11
  • 1970-01-01
相关资源
最近更新 更多