如何通过多次触摸消除冲动(SpriteKit)

How to remove an impulse with numerous touches (SpriteKit)

我对游戏做了控制,触摸的时候,角色飞到左边,第二次触摸的时候,角色飞到右边,但是当角色在飞行中,我触摸了很多次,角色向不同的方向投掷,他飞起来了,如何解决它

我希望播放器飞行时冲动不工作

enum BodyType: UInt32 {
     case playerr = 1
     case walll = 2
}

var direction: Direction?
var isInFlight = true    






override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent? {

 if isInFlight == true {
        return

    } else  {


    player?.removeAllActions()

    switch direction  {
    case .left:
        player?.run(SKAction.moveBy(x: 700, y: 0, duration: 1))
        player?.physicsBody?.applyImpulse(CGVector(dx: 700, dy: 500))

        direction = .right
    case .right:
        player?.run(SKAction.moveBy(x: -700, y: 0, duration: 1))
        player?.physicsBody?.applyImpulse(CGVector(dx: -700, dy: 
 500))

        direction     

 }

 }



 func didBegin(_ contact: SKPhysicsContact) {

    let firstBody: SKPhysicsBody
    let secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    if ( firstBody.categoryBitMask == BodyType.playerr.rawValue && secondBody.categoryBitMask == BodyType.walll.rawValue) {

        print("Add")
        isInFlight = false 

    }

}


func didEnd(_ contact: SKPhysicsContact) {

    let firstBody: SKPhysicsBody
    let secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    if (firstBody.categoryBitMask == BodyType.playerr.rawValue && secondBody.categoryBitMask == BodyType.walll.rawValue) {
        print("End")
        isInFlight = true 

    }
}

不确定您要使用您的应用程序完成什么,但这就是我使用 touchesBegantouchesEndedisInFlight 布尔值设置为 true/false 的意思.非常简单的概念,但是您在应用程序逻辑中实现它的方式取决于您。

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        isInFlight = false
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        isInFlight = true
    }