How do I give this spaceship acceleration?

后端 未结 3 1025
-上瘾入骨i
-上瘾入骨i 2020-12-19 19:52

Have a very small snippet of an asteroids-like game I\'m working on using only the DOM without Canvas. I have the \"ship\" moving pretty smoothly when arrow keys are pressed

3条回答
  •  别那么骄傲
    2020-12-19 20:33

     window.onkeydown = function( e ) {
        var kc = e.keyCode;
        e.preventDefault();
    
        if ( kc === 37 ) Keys.left++;
        else if ( kc === 38 ) Keys.up++;
        else if ( kc === 39 ) Keys.right++;
        else if ( kc === 40 ) Keys.down++;
      };
    window.onkeyup = function(e)
    {
      var kc = e.keyCode;
        e.preventDefault();
    
        if ( kc === 37 ) {Keys.left = 0;}
        else if ( kc === 38 ) Keys.up = 0;
        else if ( kc === 39 ) Keys.right = 0;
        else if ( kc === 40 ) Keys.down = 0;
    }
    function update() {
      if ( Keys.up ) {
        document.querySelector( 'div' ).style.transform += 'translateY( -'+Keys.up+'px )';
      }
      else if ( Keys.down ) {
        document.querySelector( 'div' ).style.transform += 'translateY( '+Keys.down+'px )';
      }
    
      if ( Keys.left ) {
        document.querySelector( 'div' ).style.transform += 'rotate( -'+Keys.left+'deg )';
      }
      else if ( Keys.right ) { 
        document.querySelector( 'div' ).style.transform += 'rotate( '+Keys.right+'deg )';
      }
    
        requestAnimationFrame( update );
    }
    requestAnimationFrame( update );
    

提交回复
热议问题