Reputation: 434
I am trying to make a sprite move left when touchesBegan and then move right the next time the user touches.
I have seen some code which I think would work perfectly however I am not too sure how to define "isMovingleft"
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
ship.removeAllActions()
if isMovingleft == true {
let left = SKAction.moveBy(x: 500, y: 0, duration: 5)
ship.run(left)
}
else {
let right = SKAction.moveBy(x: -500, y: -900, duration: 5)
ship.run(right)
}
isMovingleft = !isMovingleft
}
Upvotes: 0
Views: 33
Reputation: 11539
enum Direction: Int {
case left = 0
case right
}
var direction: Direction?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
ship.removeAllActions()
switch direction ?? .left {
case .left:
ship.run(SKAction.moveBy(x: 500, y: 0, duration: 5))
direction = .right
case .right:
ship.run(SKAction.moveBy(x: -500, y: -900, duration: 5))
direction = .left
}
}
Upvotes: 1