jQuery animate() 使用 keydown 进行平滑

标签 jquery jquery-animate settimeout setinterval keydown

我正在尝试做两件事,但找不到方法:

  1. 更重要的是,当按键向上时,在完全停止之前,使船舶在keydown的方向上有更多的动画,并且如果我按另一个方向,船舶将等待动画在改变方向之前完成。

2.使船舶运动更加真实,不像文本编辑器。

$(document).ready(function() {
  //Variables
  var screenWidth = $(window).width(); //Screen width
  var screenHeight = $(window).height(); //Screen height
  var shipWidth = $("#ship").width(); //Ship width
  var y = 0; //Background y position
  var currentShipPos; //ship current position

  setShipPosition(screenWidth / 2 - shipWidth / 2, screenHeight / 1.5 - shipWidth / 2);
  startBackgroundMovement();


  //Start move the screen
  function startBackgroundMovement() {
    setInterval(function() {
      y += 1;
      $('body').css('background-position-y', y + 'px');
    }, 10);
  }


  //Spaceship starting position
  function setShipPosition(posX, posY) {
    $("#ship").css("left", posX);
    $("#ship").css("top", posY);
    currentShipPos = posX;
    //alert(currentShipPos);
  }

  //Move spaceship
  $(document).keydown(function(e) {
    if (!$('#ship').is(':animated')) {
      switch (e.which) {
        case 37: //left
          currentShipPos -= 10;
          $('#ship').animate({
            left: currentShipPos + "px"
          }, 0, 'swing');

          //left edge of screen
          if (currentShipPos < 0) {
            currentShipPos = 0;
          }
          break;
        case 39: //right
          currentShipPos += 10;
          $('#ship').animate({
            left: currentShipPos + "px"
          }, 0, 'swing');
          //right edge of screen
          if (currentShipPos > screenWidth - shipWidth) {
            currentShipPos = screenWidth - shipWidth;
          }
          break;
        default:
          return;
      }
      e.preventDefault(); //not sure if needed
    }

  });


});
html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}
body {
  background-image: url('http://www.playmycode.com/dynamic/project_images/495/3495/20888.1363023335.png');
  background-repeat: repeat;
  overflow: hidden;
  background-position: 0 0;
}
#ship {
  position: absolute;
  right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <img id="ship" src="http://www.clipartbest.com/cliparts/dTr/M8g/dTrM8gnT9.png" width="40px" />
</body>

jsfiddle.net/icy1337/gw761f5w/

最佳答案

我重构了一点,去掉了$.animate(),使用这个循环来制作船舶动画:

(function shipLoop() {
    if (ship.goingLeft) {
        ship.lspeed = Math.min(ship.lspeed *1.1 || 1, ship.maxSpeed);
    } else {
        ship.lspeed = Math.max(ship.lspeed - 0.5, 0);
    }
    if (ship.goingRight) {
        ship.rspeed = Math.min(ship.rspeed *1.1 || 1, ship.maxSpeed);
    } else {
        ship.rspeed = Math.max(ship.rspeed - 0.5, 0);
    }
    ship.position = ship.position + (ship.rspeed - ship.lspeed);
    $ship.css('left', ship.position);
    requestAnimationFrame(shipLoop);
}());

按键处理程序设置这些属性,但实际上从未立即更改绘制位置,现在您还需要按键事件:

$(document).keydown(function(e){
    switch(e.which){
        case 37://left
            ship.goingLeft= true;                    
            break;
        case 39://right
            ship.goingRight= true;  
        default: return;    
    e.preventDefault();//not sure if needed
    }
}).keyup(function(e){
    switch(e.which){
        case 37://left
            ship.goingLeft= false;                    
            break;
        case 39://right
            ship.goingRight= false;  
        default: return;    
    e.preventDefault();//not sure if needed
    }
});

看看http://jsfiddle.net/gw761f5w/7/

关于jQuery animate() 使用 keydown 进行平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27545369/

相关文章:

javascript - 如何让 animation() 无穷无尽

Javascript 闭包函数不返回正确的值

javascript - 为什么我不能直接在SetTimeout函数中调用resolve()来处理Promises

JavaScript:setTimeout 不更新循环内的值

javascript - 如何在 ng-repeat 表中查找单选按钮值?

javascript - 当调用 .off() 然后调用 .on() 时,Jquery 是否可以推迟事件?

jquery - 2列;多 Accordion 面板

jQuery 循环动画每次都会暂停。如何避免停顿?

jquery - Flask AJAX 自动完成

javascript - 完成后移除动画