我正在iOS中制作一个项目,我的精灵正在向一个玩家"精灵和目标是精灵跳过弹丸而不触碰它们。我创造了一个让他跳跃的功能,但是当他离开地面而不是连续时我需要他跳一次。我需要帮助创建代码,让sprite在跳转时离开地面,但它只会在接触地面后再次跳转。
以下是我目前的代码:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let jumpUp = SKAction.moveByX(0, y: 50, duration: 0.3)
let fallBack = SKAction.moveByX(0, y: -50, duration: 0.3)
// making the player jump
player.runAction(SKAction.sequence([jumpUp, fallBack]))
}
答案 0 :(得分:0)
您只需检查player
节点当前是否有“跳转”即可轻松完成此操作。在其上运行的行动。
例如:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if player.actionForKey("jump") == nil { // check that there's no jump action running
let jumpUp = SKAction.moveByX(0, y: 50, duration: 0.3)
let fallBack = SKAction.moveByX(0, y: -50, duration: 0.3)
player.runAction(SKAction.sequence([jumpUp, fallBack]), withKey:"jump")
}
}
还值得注意的是,如果您没有更改操作的任何方面,您只需创建一次,然后在touchesBegan()
中重复使用它们。
例如:
class MyScene : SKScene {
let jumpUp = SKAction.moveByX(0, y: 50, duration: 0.3)
let fallBack = SKAction.moveByX(0, y: -50, duration: 0.3)
var jumpAction = SKAction()
override func didMoveToView(view: SKView) {
jumpAction = SKAction.sequence([jumpUp, fallBack])
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if player.actionForKey("jump") == nil {
player.runAction(jumpAction, withKey:"jump")
}
}
}