我为我的播放器实现了一次跳转。但是如果用户点击两次,我希望玩家能够双跳。
如何在下方执行此功能,检查跳转是否仍在持续时间内,以便他们可以根据需要再次点按并跳转?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
var isJumping = false
var isDoubleJumping = false
if mario.action(forKey: "jump") == nil && isJumping {
let marioTextureJump = SKTexture(imageNamed: "mariojump.png")
let animation = SKAction.animate(with: [marioTextureJump], timePerFrame: 0.6)
mario.run(animation)
mario.run(jumpAction, withKey: "jump")
if isJumping {
isDoubleJumping = true
isJumping = false
}
else {
isJumping = true
isDoubleJumping = false
}
}
}
答案 0 :(得分:3)
实现这一目标的最简单方法可能是在场景中添加2个标志
isPlayerJumping = false
isPlayerDoubleJumping = false
然后检查并在触摸功能中设置这些变量
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent {
//if the player is already double jumping don't let them jump again until finished
guard !isDoubleJumping else { return }
let animation = SKAction.animate(with: [playerTextureJump], timePerFrame: 0.6)
player.run(animation)
if isJumping {
isDoubleJumping = true
isJumping = false
player.run(jumpAction, withKey: "jump") {
self.isDoubleJumping = false
}
}
else {
isJumping = true
player.run(jumpAction, withKey: "jump") {
self.isJumping = false
}
}
}
修改
如果你不想使用警卫,你可以放置一个if statement
if isPlayerDoubleJumping {
return
}
在jumpAction结束时,你必须运行一段代码来重置变量
SKAction.run {
isJumping = false
isDoubleJumping = false
}
你甚至可以在触摸中改变内联变量,就像这样......
player.run(jumpAction, withKey: "jump") {
self.isPlayerJumping = false
self.isPlayerDoubleJumping = false
}