Jump to content

Can't pause html5 canvas game


Assadhere
 Share

Recommended Posts

Hello,

 

I've been searching the internet for a solution to my game. My idea was to pause/resume my game with the 'p' button on my keyboard, which has keycode 80.

 

However, everytime I try to apply a found solution from the internet to my game, it doesn't work or my game doesn't even start. I tried the debugger, I tried using other ways.

 

Is there anyone that can guide me through this or atleast tell me how to do it properly?

 

I've been searching for hours now, and this is an assignment for school.

 

 

Thanks in advance,

Assad

 

Link to comment
Share on other sites

you dont really describe what works, so it's hard to help you. does your keyevent fire (use the debugger)? are you using jquery or simple javascript. maybe post some code

I'm simply using jquery. I tried this:

 

//////////////////////////////////////////////////////////////

window.addEventListener('keydown', pauseGameKeyHandler, false);

function pauseGameKeyHandler(e) {

      var keyCode = e.keyCode;

      switch(keyCode){

        case 80: //p

          togglePause();

          break;

      }

    }

    function togglePause(e) {

        if (keyCode == 80) {

            pause();

        } else if (!paused) {

            pause();

        } else if (paused) {

            resume();

        }

    };

    function pause() {

        paused = true;

        pauseTime = Date.now();

        var pausedElement = document.getElementById( 'paused' );

        if( pausedElement ) {

            pausedElement.style.width = world.width + 'px';

            pausedElement.style.height = world.height + 'px';

        }

        document.body.className = 'paused';

    }

    function resume() {

        var wasPaused = paused;

        paused = false;

        time += Date.now() - pauseTime;

        if( wasPaused ) {

            timeLastFrame = Date.now();

            animate();

        }

        document.body.className = '';

    }

///////////////////////////////////////////////////////////////

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...