问:如何制作它让玩家可以跳到我点击的任何地方,但在触摸时不会继续向上移动?

时间:2022-02-18 00:40:23

Im creating a game like Doodle jump, and I want it to be so that my hero does jump to wherever I tap, but when I hold that touch on the screen, it just keeps moving up until it is off screen, which isn't what I'm aiming for. I just want it to be so that no matter how much I tap and hold, my hero does not keep moving up.

我创造了一个像涂鸦跳跃的游戏,我希望它能让我的英雄跳到我点击的任何地方,但是当我在屏幕上按住触摸时,它只是一直向上移动,直到它离开屏幕,这不是我的目标是什么。我只是希望它能够如此无论我多少按住它,我的英雄都不会继续前进。

Here is my code for my touches functions:

这是我的触摸功能的代码:

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    player.animatePlayer()
    let touch = touches.first as? UITouch
    let touchLocation = touch!.locationInNode(self)
    lastTouch = touchLocation
        player.physicsBody?.categoryBitMask = snowCategory
        player.physicsBody?.contactTestBitMask = playerCategory
        player.physicsBody?.affectedByGravity = true
        player.physicsBody?.collisionBitMask = playerCategory
        player.physicsBody?.usesPreciseCollisionDetection = true



    }




   override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    lastTouch = nil



}



  override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    lastTouch = nil
    }
      override func update(currentTime: NSTimeInterval) {
    if let touch = lastTouch {
        var xForce: CGFloat = 0.0
        var yForce: CGFloat = 0.0
        let xTouchOffset = (touch.x - player.position.x)
        let yTouchOffset = (touch.y - player.position.y)

        if xTouchOffset > 0.0 {
            xForce = xPlayerForce
        } else if xTouchOffset < 0.0 {
            xForce = -xPlayerForce
        } // else we do nothing

        if yTouchOffset > 0.0 {
            yForce = yPlayerForce
        } else if xTouchOffset > 0.0 {
            yForce = -yPlayerForce
        }
        // here you can choose whether you want it to push
        // the player node down, using similar code from the
        // above if statement

        let impulseVector = CGVector(dx: xForce, dy: yForce)
        player.physicsBody?.applyImpulse(impulseVector)
    }


   }

1 个解决方案

#1


0  

It seems your object movement is activated when you lift your finger. Instead of doing this, I would say put your code in touches began, or at least have some way of stopping the object when it gets where you want. Right now you have it in an update method that continues to run after you lift your finger, so that's why it keeps going.

抬起手指时,您的物体移动似乎已激活。而不是这样做,我会说你的代码开始接触,或者至少有一些方法可以在它到达你想要的地方时停止它。现在你有一个更新方法,在你举起手指后继续运行,这就是为什么它继续前进。

#1


0  

It seems your object movement is activated when you lift your finger. Instead of doing this, I would say put your code in touches began, or at least have some way of stopping the object when it gets where you want. Right now you have it in an update method that continues to run after you lift your finger, so that's why it keeps going.

抬起手指时,您的物体移动似乎已激活。而不是这样做,我会说你的代码开始接触,或者至少有一些方法可以在它到达你想要的地方时停止它。现在你有一个更新方法,在你举起手指后继续运行,这就是为什么它继续前进。