javascript - Stop awaiting input after fixed time -


i'm new web programming , stumbled upon problem couldn't solve , i'm not sure can solved. i'm creating simple "game" using jquery, , want make thread stop waiting (keydown) input , carry on code, can perform either simple upwards "jump", or " left jump"/"right jump". can done?
here follows codebit i've been doing far:

http://www.codecademy.com/pt/pyslayer10761/codebits/oyq11a/edit

you need game loop thats running independantly keydown-handler. elsewise animation might hack keydown handler might stop moment no inputs made anymore.

by looking @ code, can see tried creating new settimeout() on keydowns. creating every keydown event fired. crash/freeze browser @ point if engine not realize creation same timeout on , on again.

do this: in onkeydown handler set variable keydowncode keycode value. create new game loop this

<script> var keydowncode = 0; var isinanimation = false; var animatedkeydowncode = 0; var animationstarttime = 0; var animationstartvalue = 0;  // lightweight keydown handler: $(document).keydown(function(key) {   keydowncode = parseint(key.which,10); } $(document).keyup(function(key) {   keydowncode = 0; }  function animation() {    // here comes animation logic..    // e.g. keep moving 100 200 milliseconds after keypress    // milli secs difference    var nowtime = date.now();    // animations prioritized: 1 animation @ same time!   if(isinanimation) {     switch(animatedkeydowncode) {       case 37:         var delta = (nowtime-animationstarttime)/100*10;         if(delta > 10) { delta = 10; isinanimation = false; }; // animation over!         $('img').left(animationstartvalue-delta);       case 37:         var delta = (nowtime-animationstarttime)/200*10;         if(delta > 10) { delta = 10; isinanimation = false; }; // animation over!         $('img').top(animationstartvalue-delta);       case 39:         var delta = (nowtime-animationstarttime)/100*10;         if(delta > 10) { delta = 10; isinanimation = false; }; // animation over!         $('img').left(animationstartvalue+delta);     }     // ready take new input, if not outdated   } else {      // if key down , no animations active     if(keydowncode > 0) {       switch(keydowncode) {         case 37:           animationstarttime = nowtime;           animatedkeydowncode = keydowncode;           animationstartvalue = $('img').left();           isinanimation = true;          case 38:            // start jump if on bottom           if($('img').css("top") == '390px') {             animationstarttime = nowtime;             animatedkeydowncode = keydowncode;             animationstartvalue = $('img').top();             isinanimation = true;           }          case 39:           animationstarttime = nowtime;           animatedkeydowncode = keydowncode;           animationstartvalue = $('img').left();           isinanimation = true;        }     }   }   window.requestanimationframe(animation); } window.requestanimationframe(animation); </script> 

this no full game, need adjust working game. e.g. might want add gravity mario down again when there no input..


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -