I\'m making a game using sprite kit and I want my Character to move across the screen when you hold down the left/right move button. The problem is that he only moves when t
You know when the button is touched because touchesBegan
is called. You then have to set a flag to indicate that the button is pressed.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
if leftButton.containsPoint(touch.locationInNode(self)) {
leftButtonIsPressed = true
}
if rightButton.containsPoint(touch.locationInNode(self)) {
rightButtonIsPressed = true
}
}
In update()
, call your function that flag is true:
update() {
if leftButtonIsPressed == true {
moveLeft()
}
if rightButtonIsPressed == true {
moveRight()
}
}
You set the flag to false when touchesEnded
is called for that button:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
if leftButton.containsPoint(touch.locationInNode(self)) {
leftButtonIsPressed = false
}
if rightButton.containsPoint(touch.locationInNode(self)) {
rightButtonIsPressed = flase
}
}
Edit:
As pointed out by KoD, a cleaner way to do this (for movement buttons) is with SKAction
which removes the need for the flag:
SKActions
for moveTo x:0
and moveTo x:frame.width
in
didMoveTo(View:)
touchesBegan
, run the correct SKAction on the correct object
specifying a key for the SKAction.touchesEnded
, remove the relevant SKAction.You'll have to do some maths to calculate how many points your object will have to move and then set a duration for the SKAction based upon this distance and a movement speed (in points per second).
Alternatively, (thanks to KnightOfDragons for this) create a SKAction.MoveBy x:
which moves a small distance (based upon your desired movement speed) and with a duration of 1/60s. Repeat this action forever (SKAction.repeatForever
) when the button is touched and remove the repeating SKAction when the button is released.