我正在开发一个精灵套装游戏,我有一个名为英雄的节点,它正在接近恶棍。我对applyImpulse
的跳跃动作有一个问题,在这种情况下,英雄可以反复跳跃而不是躲避恶棍。我用一个布尔变量来改变状态,用一个定时器值在3秒内只跳一次,但是没有用。这是我的代码
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if isGameOver == true {
self.restart()
}
if isstarted {
self.runAction(SKAction.playSoundFileNamed("jump.wav", waitForCompletion: false))
for touch: AnyObject in touches{
if jumpon == false { //hero has not jumped
let location = touch.locationInNode(self)
heroAction()
jumpon = true //hero jumped
}
}
} else {
isstarted = true
hero.stop()
hero.armMove()
hero.rightLegMove()
hero.leftLegMove()
treeMove()
villanMove()
let clickToStartLable = childNodeWithName("clickTostartLable")
clickToStartLable?.removeFromParent()
addGrass()
// star.generateStarWithSpawnTime()
}
}
func changeJump(){
jumpon = false // hero has landed
}
每秒调用函数更新,然后必须调用changeJump
override func update(currentTime: CFTimeInterval) {
if isstarted == true {
let pointsLabel = childNodeWithName("pointsLabel") as MLpoints
pointsLabel.increment()
}
if jumpon == true { // checked everyframe that hero jumped
jumpingTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "changeJump", userInfo: nil, repeats: true) // changing jump status
}
}
我应该如何更新我的代码,使英雄只在三秒内跳一次。提前谢谢。
答案 0 :(得分:3)
一些想法......
SKAction
或者两个而不是NSTimer
。暂停/恢复场景和/或查看时,SKAction
暂停/恢复正常update
方法被称为〜每秒60次而不是每秒update
检查跳转状态和一些代码......
if (!jumping) {
jumping = true
let wait = SKAction.waitForDuration(3)
let leap = SKAction.runBlock({
// Play sound and apply impulse to hero
self.jump()
})
let action = SKAction.group([leap, wait])
runAction(action) {
// This runs only after the action has completed
self.jumping = false
}
}