I have a little Space Invaders game and I added two buttons leftButton and rightButton and I want the ship to move as long as one of the buttons is touched AND HELD in the proper direction.
I made it move in the right direction but I have to tap the button repeatedly, but I want ship to move WHILE the player is holding the button.
For simplicity I only posted code for the left button as I believe the way to code the other button is identical:
class GameScene: SKScene {
var leftButton = SKSpriteNode()
var ship = SKSpriteNode()
override func didMove(to view: SKView) {
leftButton = self.childNode(withName: "left") as! SKSpriteNode
ship = self.childNode(withName: "player") as! SKSpriteNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointTouched = touch.location(in: self)
if leftButton.contains(pointTouched) {
player.position.x -= 30
}
}
}
}