I have two nodes node1 and node2. node1 is at position 0. when I click on the screen it run its action until its in the middle of the screen or the user removes their finger from the screen. Once node1 reaches the middle of screen it will stop, and node2 will run its action. My code does this so far.
However, in order for node2 to run its action the user must remove their finger from the screen and touch it again. I do not want this to happen. I want the action for node2 to run after node1 finishes without the user removing their finger from screen.
Basically, the user is holding down the screen node1 runs its action finishes and then node2 runs its action.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let Node1Move=SKAction.moveBy(x:50,y:0,duration:0.6)
let node1rep=SKAction.repeatForever(Node1Move)
node1.run(node1rep)
if modeNode2==1{
let node2move=SKAction.moveBy(x:-10,y:0,duration:0.9)
let node2Rep=SKAction.repeatForever(node2move)
node2.run(node2Rep)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
node1.removeAllActions()
node2.removeAllActions()
}
override func update(_ currentTime: TimeInterval) {
if node1.position.x+node1.frame.width > self.frame.width/2{
node1.removeAllActions()
moveNode2=1
}
}
touchesBegin will only fire once, so your moveNode2 == 1 will not do anything. Instead of using moveBy, user moveTo, and have node1 moveTo the center of the screen - node1 width. Then just add a completion block to the node1.run.
Note: you may need to change the duration, not sure where these numbers come from. If you plan on the user to constantly be tapping the screen, then just do totalDuration = 3 * (node1.x - 'node1 start x')/((self.frame.width/2 - node1.frame.width) - 'node1 start x')
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let Node1Move=SKAction.moveTo(x:self.frame.width/2 - node1.frame.width,y:0,duration:3.0)
node1.run(Node1Move)
{
let node2move=SKAction.moveBy(x:-10,y:0,duration:0.9)
let node2Rep=SKAction.repeatForever(node2move)
node2.run(node2Rep)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
node1.removeAllActions()
node2.removeAllActions()
}