swift - 如何根据触摸屏的时间使 Sprite 跳得更高/更小?

标签 swift sprite-kit game-physics

嗨,我很难找到正确的代码来应用于我的 Sprite ,以允许仅在粘贴时进行小跳跃,而当手指在屏幕上停留较长时间时允许进行更高的跳跃。 (请在下面找到当前代码)

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

最佳答案

您可以使用更新方法。在你的touchesBegan方法中,你设置一个 bool 值或类似的东西来显示你仍在屏幕上按下的update方法。例如:

//touchesBegan
touching = true

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

//touchesEnded
touching = false

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

关于swift - 如何根据触摸屏的时间使 Sprite 跳得更高/更小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666375/

相关文章:

swift - 如何更改不同 View Controller 上的文本颜色

ios - 在 Swift 的数组中查找 SKSpriteNode 的索引

ios - 场景大小不适合屏幕 SpriteKit

c++ - 我对带有增量时间和插值的固定时间步的解决方案是否错误?

ios - 如何在 Swift 中终止(不是 segue)一个 View

swift - 具有并发和串行的异步任务

ios - 当 SceneKit 动画停止时,SpriteKit 的动画覆盖在 SceneKit 上停止

javascript - 如何强制处理正确处理运动的负值?

python - 碰撞Pygame后穿过墙壁

swift - 如何在 Swift 中使用计时器避免 "variable was written to, but never read"警告