I made control for the game when you touch, the character flies to the left side, when touching the second time the character flies to the right side , but when the character is in flight and I touch many times, the character throws in different directions, and he flies up, How to fix it
I want the impulse not to work when the player is in flight
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
}
}
Not sure exactly what you are trying to accomplish with your app but this is what I mean by setting the isInFlight
bool to true/false using touchesBegan
and touchesEnded
. Very simple concept, but they way you implement it in your app logic is up to you.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isInFlight = false
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
isInFlight = true
}