1

I need help incorporating the

PreformSelector: WithObject: AfterDelay:

With the SKScene being paused. I am making a game where there is a pause button which pauses the scene. This, however does not affect the

PreformSelector: WithObject: AfterDelay:

function which calls the "Reload" function. Without this fixed the user can fire a shot, press pause, wait for the reload function to be called, un-pause, and shoot. This makes it so the user can continuously fire without having to reload in "game time". Is there any way to fix this?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
ManOfPanda
  • 35
  • 7
  • possible duplicate of [Spritekit - Creating a timer](http://stackoverflow.com/questions/23978209/spritekit-creating-a-timer) – CodeSmile Aug 18 '14 at 14:39

1 Answers1

3

You shouldn't be using performSelector:withObject:afterDelay:, because keeping track of timing and storing the selectors and objects etc. with that method would be very cumbersome.

Instead, make use of SpriteKit's +[SKAction waitForDuration:] action.

You would want to do something like the following (I'm assuming this code takes place somewhere in one of your scene's methods):

// Replace 2.0 below with however long you want to wait, in seconds
SKAction *waitAction = [SKAction waitForDuration:2.0];

// I'm assuming your "Reload" method is a method declared in your scene's
// class and not some other class, so I'm using "self" as the target here.
SKAction *reloadAction = [SKAction performSelector:@selector(Reload) onTarget:self];

SKAction *sequenceAction = [SKAction sequence:@[waitAction, reloadAction]];

// Since I'm assuming this is in your scene implementation, `self` here
// refers to your scene node.
[self runAction:sequenceAction];

Since actions can be paused and resumed, when you pause your scene with self.paused = YES; your actions will also get paused and will resume where they left off when you later unpause your scene.

TylerP
  • 9,600
  • 4
  • 39
  • 43