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

时间:2015-09-14 16:40:34

标签: swift sprite-kit

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

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

 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 个答案:

答案 0 :(得分:0)

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