Assadhere Posted April 21, 2015 Share Posted April 21, 2015 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 Quote Link to comment Share on other sites More sharing options...
marcgfx Posted April 22, 2015 Share Posted April 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
Assadhere Posted April 22, 2015 Author Share Posted April 22, 2015 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 codeI'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 = ''; }/////////////////////////////////////////////////////////////// Quote Link to comment Share on other sites More sharing options...
marcgfx Posted April 22, 2015 Share Posted April 22, 2015 does your pauseGameKeyHandler ever fire? it should fire no matter which key you press. this should show you the keycode and is a little more jquery style:$(window).on("keydown", function(e){alert(e.keyCode);}); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.