Jump to content

Javascript Game


tyoo
 Share

Recommended Posts

Hi,

 

I´m programing a javascript game, but i´cant find out how to do a pause/play on it.

 

I alredy find a way to stop all interval´s and timeout´s but i can´t start them all, just if i do it one by one.

 

To stop them all e do the next:

 

var highestTimeoutId = setTimeout(";");

for (var i = 0; i < highestTimeoutId; i++) {
clearTimeout(i);

 

Exist some way to do it with jquery or something else?

 

Thanks :)

Link to comment
Share on other sites

Hello,

I'm having troubles understanding what exactly you are looking for here. If you set timeouts up and than clear them then you obviously need to start new ones in the same way as you did at the beginning - as you said one by one, logic dictates there is no other way around it.

Depending on your game you can integrate pause function into your game in a simplier way, the simplest one would be to just set some variable to some value and wrapping everything in your loops in if condition, such as this (though not perfect):

var obj = {pause: false};for () {  // here starts the game loop  if (!obj.pause) {    // here comes all the game code, when the game is paused obj.pause = true, thus all the game code is ignored, thus the game isn't progressing until you change obj.pause back to false  }}  // here ends the game loop// if you use this way you don't need to stop intervals

Well this is just one simple way how to integrate puse into your game, depending on how complex your game is you might want to do it in some other way.

Here is I guess a similar question about pausing a timeout in javascript.

(btw are you sure you need timeouts in your game, perhaps changing the logic to requestAnimationFrame would make your life easier as it is nowadays preferable way to do game loops in browsers - just curious?)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...