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

时间:2017-12-30 04:53:34

标签: ios swift sprite-kit touchesbegan

当你触摸时我控制了游戏,角色飞到左侧,当第二次触摸角色飞到右侧时,但是当角色在飞行中并且我触摸了很多次时,角色抛出在不同的方向,他飞起来,如何解决它

我希望当玩家在飞行中时,冲动不起作用

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 

    }
}

1 个答案:

答案 0 :(得分:0)

不确定您要使用自己的应用尝试完成什么,但这就是我的意思是使用isInFlighttouchesBegantouchesEnded bool设置为true / false。非常简单的概念,但是你在app逻辑中实现它的方式取决于你。

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

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