I'm a bit stuck at how to approach the following issue:
My scrolling game spawns obstacles of different kinds that are loaded from a plist. The spawning is delayed by an SKAction, the duration of which for each obstacle is computed by the y position.
Updated to reflect Wraithseeker's answer.
This is the setup code where all timers are started in the
didMoveToView()
:
for obstacleTuple in level.obstacleTuples {
let positionY = obstacleTuple.positionY
let obstacle = obstacleTuple.obstacle as! SKNode
let duration = NSTimeInterval(positionY / kObstacleDistancePerSec)
// **Update**: Instead of running the wait action on the scene...
//runAction(SKAction.waitForDuration(duration), completion: {
// I should run it on the foregroundNode...
foregroundNode.runAction(SKAction.waitForDuration(duration), completion: {
self.foregroundNode.addChild(obstacle)
})
}
// And then toggle the pause property on that node to stop and resume.
let foregroundNode.paused = foregroundNode.paused == true ? false : true
I'd like to have special events (which occur depending on user action), during which the spawning of above obstacles is further delayed and the player can collect bonus objects.
So basically, I'd need to assign a key to the wait SKAction and then have a .pauseActionForKey()
method (instead of .removeActionForKey()
).
Is it possible to extend SKAction
with that?
Are there better ways to achieve what I'm trying to do?
One way to solve this will be to create an empty
SKNode
and assign those action to that node which you want to be paused. This is so that when you pause the actions, it does not pause your whole game since you are assigning theSKActions
to the scene at the moment usingself
.To pause the actions, simply call pause upon your empty
SKNode
as suggested by sangony.An example