如何使用 sprite kit 场景同时动画和移动播放器节点?
How to animate and move player node simultaneously, using sprite kit scene?
我正在开发一款小型 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
我阅读了 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")
}
我正在开发一款小型 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
我阅读了 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")
}