Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by blindMoe

I know this is newbie stuff but I am a newbie and wanted to share this code with other newbies who wanted to easily add a Pause option to their game.

In my main.js file I added the two following properties to the class:

paused: false,
pausing: false,

then in the init() function I added:
		ig.input.bind(ig.KEY.P, "pause");

In the same file, in the update method() I added this at the very top:

		if(!ig.input.state('pause') && this.pausing) this.pausing=false;

		if (ig.input.state("pause")) {
			if (!this.pausing) {
				this.paused = (this.paused) ? false : true;
				this.pausing = true;
			};
		};

		if (this.paused) return;

In that code we use the this.pausing property to have this section of code only be called once when the P key is pressed. Without this code if you did not tap the P key very quickly it would get called and pause / unpause the game every other frame.

That is technically all you need but I also added this to the draw() method in the same file to alert to the user that the game is paused and to stop impact from drawing entities when it doesn't need to ( eating up idle cpu ).

		if (this.paused) {
			this.font.draw(" - Paused - ", ig.system.width/2, 232, ig.Font.ALIGN.CENTER);

			// Return to stop anything else drawing
			return;				
		}

I hope this helps someone! Eventually I will most likely make a small plugin with this code but that is for a later date.

1 decade ago by Jerczu

Good job. Didn't test your approach but it seems to be right.

Dont really see the point of having two pausing properties but that's my only nitpick.

1 decade ago by fugufish

Hmm you seem to be creating a shortcut 'return' in the draw loop. Check your CPU, it'll still be running. You're just drawing a pause text in the middle of the screen.

to make a pause function that really works, try using


// for pausing
ig.system.stopRunLoop.call(ig.system);

// for resuming
ig.system.startRunLoop.call(ig.system);

1 decade ago by Jerczu

Quote from fugufish
Hmm you seem to be creating a shortcut 'return' in the draw loop. Check your CPU, it'll still be running. You're just drawing a pause text in the middle of the screen.

to make a pause function that really works, try using

##

// for pausing
ig.system.stopRunLoop.call(ig.system);

// for resuming
ig.system.startRunLoop.call(ig.system);

##


Seem to be much cleaner and gracious approach - good stuff :)

1 decade ago by dominic

Minor nitpick: you don&039;t need the #.call(ig.system) if you call the functions on the ig.system object anyway. This will do as well:

// for pausing
ig.system.stopRunLoop();

// for resuming
ig.system.startRunLoop();

You would need the .call(ig.system) if you do something like this:
var pauseFunction = ig.system.stopRunLoop;

// This won't work, because the "this" object inside the function will 
// be "window". I.e. the function doesn't know which object it's being 
// called on and defaults to the global "window" object.
pauseFunction();

// This works again. It explicitly sets the "this" to "ig.system":
pauseFunction.call(ig.system);

1 decade ago by Jerczu

Awesome!

1 decade ago by potan

Dominic, i think the documentation is missing stopRunLoop & startRunLoop method:

http://impactjs.com/documentation/class-reference/system

Could you update those for future reference.
In case this thread is lost in the sea of.... threads :P

1 decade ago by paulh

More noobie stuff .. im guessing this doesnt work because the key press "O" isnt tracked as the game is paused??? How do i make it so the O press is recognised in paused mode?

		ig.input.bind(ig.KEY.P, 'pause');
		ig.input.bind(ig.KEY.O, 'unpause');


if (ig.input.state("unpause"))
      {
	ig.system.startRunLoop();
	alert(ig.input.state);
      }
      
else if (ig.input.state("pause"))
      {
	ig.system.stopRunLoop();
      }

Or is this code nonsense?

1 decade ago by Arantor

You can't, once the run loop is stopped, ig.input won't be evaluated. What you have to do is have the trigger somewhere outside Impact.

Perhaps throw up a paused screen with a button on it that when pressed, calls startRunLoop() again. Or on pausing, bind the keypress event to a separate function so that it can call startRunLoop().

1 decade ago by paulh

i wish i knew exactly what u meant :-(

thanks though!

1 decade ago by Arantor

Which part was confusing?

1 decade ago by paulh


What you have to do is have the trigger somewhere outside Impact.


If im using a gameframework, how would i do something outside of it?
Would it still work if im using impactios?
How can i call a function startrunloop outside of the framework that uses startrunloop?

1 decade ago by Arantor

No, it won't work in iOSImpact.

As far as calling it outside the framework, you can call it directly, the main game is accessible as ig.game, so if your main game has an unpause function, it will be callable as ig.game.unpause() from outside.

1 decade ago by paulh

hmm so whats the best way of doing this to also work in iosimpact?

1 decade ago by Arantor

Doing it all manually, not calling stopRunLoop() but simply storing a 'is paused' variable somewhere and in the event of being paused, do nothing.

1 decade ago by TigerJ

hmm so whats the best way of doing this to also work in iosimpact?


This is what we use in our main js for cloudy using the input event for our desired pause key to set ig.game.paused to true. the same entity that listens for the pause also has ignorePause as true to allow it to unpause.

Additionally timers work outside the update() and if you use timers for scoring or to measure you will need to take a "snapshot" of the time during the pause or start a timer to use as an offset.

if( this.paused ) {
        // only update some of the entities when paused:
			for( var i = 0; i < this.entities.length; i++ ) {
				if( this.entities[i].ignorePause ) {
					this.entities[i].update();
				}
			}
		}
		else {
			// call update() as normal when not paused
			this.parent(); 
		}

*I don't use the ios so I am not sure if it works in there, this is just an alternative that keeps the pause code inside the impact and game library

*also I may have learned about this method from another post/person here and thanks if I did and you are reading this :)

1 decade ago by Karios

I have an issue with the second method mentioned here (the one with ignorePause). Even though the entities do not update, it seems like all my animations mess up while the game is paused, most likely because the global timer progresses normally and the animation frames are based on that timer. So all animations are already finished when I unpause, and if I am counting on .currentAnim.loopCount I am pretty much out of luck, since there is no telling what this is (depends on how long was the game paused for).
Any ideas? Can I freeze the counter the game relies for animation also when pausing or will this disable my ability to unpause?

1 decade ago by alexandre

I just saw this now and assume you made your own solution long ago.

Here's what I do to pause/unpause my game:
// main.js

pause: function()
{
  ig.Timer.timeScale = (ig.Timer.timeScale == 0 ? 1 : 0);
  this._paused = ig.Timer.timeScale == 0;
},

The _paused flag is used to, while keeping touch handling alive, hence allowing one to unpause the game, block normal--i.e., when game is not paused-- touch handling.

1 decade ago by gort

Awsome! SO helpful! had a problem with that. suggestion :
if (ig.input.state("pause")) {
            if (!this.pausing) {
                this.paused = !this.paused;
                this.pausing = true;
            };
        };

Thats a cleaner and faster way.

1 decade ago by dmen

Quote from potan
Dominic, i think the documentation is missing stopRunLoop & startRunLoop method:

http://impactjs.com/documentation/class-reference/system

Could you update those for future reference.
In case this thread is lost in the sea of.... threads :P



Still doesn't - two years later.
Page 1 of 1
« first « previous next › last »