AjayPandiyaraj
AjayPandiyaraj

Reputation: 80

Applying impulse on a SpriteKit node

I'm developing a Sprite Kit Game, which I have a node named hero who dodges approaching villains. I have an issue with applyImpulse for a jump action and, in this case, the hero can jump repeatedly and fly instead of dodging the villains. I'm used a boolean variable to change the status with a timer value to jump only once in 3 seconds but that is not working. Here is my code

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
}

and function update is called in every second and then changeJump must be called

 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
    }
}

How should I update my code to make the hero jump only once in three seconds. Thanks in advance.

Upvotes: 2

Views: 590

Answers (1)

0x141E
0x141E

Reputation: 12753

Some thoughts...

  1. I suggest you use an SKAction or two instead of an NSTimer in Sprite Kit games. SKActions pause/resume appropriately when you pause/resume the scene and/or view
  2. The update method is called ~60 times a second not every second
  3. Checking the status of a jump in update is not need
  4. Alternatively, you can check if the hero is on the ground before starting another jump sequence instead of using a timer. The game may frustrate the user if the hero is on the ground (and ready to jump) but the timer is still counting down

and some code...

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
    }
}

Upvotes: 3

Related Questions