在SpriteKit节点上应用脉冲

时间:2015-08-04 13:03:39

标签: ios swift sprite-kit

我正在开发一个精灵套装游戏,我有一个名为英雄的节点,它正在接近恶棍。我对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
    }
}

我应该如何更新我的代码,使英雄只在三秒内跳一次。提前谢谢。

1 个答案:

答案 0 :(得分:3)

一些想法......

  1. 我建议你在Sprite Kit游戏中使用SKAction或者两个而不是NSTimer。暂停/恢复场景和/或查看时,SKAction暂停/恢复正常
  2. update方法被称为〜每秒60次而不是每秒
  3. 不需要update检查跳转状态
  4. 或者,你可以在开始另一个跳跃序列而不是使用计时器之前检查英雄是否在地面上。如果英雄在地上(并准备跳跃),但计时器仍在倒计时,游戏可能会让用户感到沮丧
  5. 和一些代码......

    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
        }
    }
    
相关问题