2
votes

As the question states, in spritekit I am trying to find a way to change my value of a bool from false, too true for x amount of seconds then change it back to false. the logic is i have this var, isStunned, when my player comes in contact with x sprite i call it and my joystick is disabled since the player is "stunned"

 if (controlButtonDown) { // If i pressed joystick
    if (!isStunned) { // IF i am not stunned
  //move player

When my player touches the glue, i set isStunned to yes and the joystick is disabled, however I am wondering of a way to only disable my joystick or not move my sprite for x amount of seconds. I know of SKAction and the wait sequence, but those are actions and I dont think they will apply here. Any help is appreciated.

Thank you

3
check NSTimer functionality... may be that fits your situationSaad Chaudhry
Thanks all for pointing me in the right directionSleep Paralysis
use an SKAction runBlock with waitforduration action wrapped in repeatforever. Don't use NSTimer as timers won't automatically pause when pausing the view, scene or node or when running a transition.LearnCocos2D
@LearnCocos2D I was thinking of that, but i dont want to run an action on a sprite..."per say". the moving or not moving of the sprite is dictated by a variable.Sleep Paralysis

3 Answers

3
votes

Since you ask specifically with a spritekit tag.

Don't use NSTimer or actions to work with time in a game. Instead use the update function. If you're running at 60fps that means the update function will be called 120 times for two seconds. Using the frames instead of seconds to keep your game updated will avoid the gameplay being changed by lag. Imagine for instance that the players game lags a little, in other words runs slower. 2 seconds is still 2 seconds regardless of game lag, so now he is affected less by the glue than a person who has no lag. So...

First off you should have a variable keeping track of in game time: int _time;

In every update cycle you add one to time: _time++;

You should have a variable keeping track of when the user touched the glue: int _glueTouchedAtTime;

When the user clicks glue you set: _glueTouchedAtTime = time;

You should have a constant defining how long the glue is active: #define GLUE_TIME 120;

When you initiate the game set _glueTouchedAtTime to: _glueTouchedAtTime = -GLUE_TIME; To prevent the glue from being on when (_time == 0)

Testing if the user has touched glue now works like this:

if(_glueTouchedAtTime+GLUE_TIME>time) {
    // Glue is active
} else {
    // Glue is not active
}

Different glues for different sprites

To have different glues for different sprites I would suggest first doing a general game object (GameObject) as a subclass of either SKNode or SKSpriteNode depending on your needs. Then create a subclass of GameObject called GameObjectGluable. This should have a property called: @property int gluedAtTime;

You glue the glueable game object by: aGameObjectGluable.gluedAtTime = time;

Now you can test:

if(aGameObjectGluable.gluedAtTime +GLUE_TIME>time) {
  // Game Object is glued
} else {
  // Game object is not glued
}
0
votes

Set your value-of-interest to true and then fire a NSTimer after two seconds to set it back to false.

For an explanation of how you might use NSTimer, see this SO thread: How do I use NSTimer?

0
votes

You can use SKAction also. The runBlock method let's you execute blocks of code as actions.

SKAction *wait = [SKAction waitForDuration:2.0];
SKAction *reenableJoystick = [SKAction runBlock:^{
    joystick.userInteractionEnabled = TRUE;
}];
SKAction *sequence = [SKAction sequence:@[wait, reenableJoystick]];
[self runAction:sequence];