如何根据触摸屏的时间量使精灵跳得更高/更小?

时间:2015-04-16 05:54:04

标签: swift sprite-kit game-physics

嗨,我很难找到适用于我的精灵的正确代码,只允许在录音时进行小跳跃,而当手指在屏幕上的时间更长时跳跃更高。 (请在下面找到当前代码)

 override func touchesBegan(touches: Set<NSObject>, withEvent event:UIEvent) {



    /* Called when a touch begins */

    if (gameOver == 0){

        //Player Begin Jumping.
        player.physicsBody?.applyImpulse(CGVectorMake(0, 200))
        player.runAction(SKAction .playSoundFileNamed("sounds/Jump.caf", waitForCompletion: true))

    }

}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    if (gameOver == 0){

        //Player End Jump.
        player.physicsBody?.applyImpulse(CGVectorMake(0, -120))

2 个答案:

答案 0 :(得分:1)

您可以使用update - 方法。并且在你的touchesBegan方法中你设置一个布尔值或类似的东西来显示你仍然按在屏幕上的update方法。例如:

//touchesBegan
touching = true

//update-method
if touching {
    player.physicsBody?.applyImpulse(CGVectorMake(0, 1))
}

//touchesEnded
touching = false

您必须更改applyImpulse才能满足您的需求。

答案 1 :(得分:0)

这实际上非常简单,这意味着当玩家放下屏幕时,他会摔倒,如果他坚持,他将达到200,否则他会跌倒

touches began {
    sprite.physicsbody.applyImpulse(CGVector(dx: 0 dy: 200)
}

touches eneded {
    sprite.physicsbody.applyImpulse(CGVector(dx: 0 dy: -57)
}