2

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

Moon Cat
  • 2,029
  • 3
  • 20
  • 25
Sleep Paralysis
  • 449
  • 7
  • 22

3 Answers3

3

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
}
Theis Egeberg
  • 2,556
  • 21
  • 30
  • works perfectly, except that i have an issue on this line if ((_glueTouchedAtTime+GLUE_TIME) > _time) it works if i remove "GLUE_Time and put 120". but like this it's saying "if body is empty" – Sleep Paralysis May 01 '14 at 13:49
  • Also, say i want different "glues" to stick my sprite for different times. is it wise to over ride the update function and say something like this. -(void)update:(NSTimeInterval)currentTime timestuck:(float)timestuck{ – Sleep Paralysis May 01 '14 at 13:57
  • GLUE_TIME is a defined constant, so it needs to be defined before you use it. You can use a hardcoded int here if you like, but I recommend having a file which holds constants like these. I've edited my answer to reflect your comment about multiple glues. – Theis Egeberg May 01 '14 at 16:19
0

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?

Community
  • 1
  • 1
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
0

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];
ZeMoon
  • 20,054
  • 5
  • 57
  • 98