如何通过requestAnimationFrame从新实例移动形状?

时间:2016-03-02 07:06:37

标签: javascript html5 canvas html5-canvas html5-animation

这是fiddle。 当按下空格键(键代码32)时,将创建小矩形以模拟子弹。我遇到了一些问题:如何将它们移动到顶部(减小y坐标)? 谁能帮我? THX!

window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var cw = canvas.width;
var ch = canvas.height;
var ps = false;

init();

function init(){
    context.rect((cw-5)/2, ch-5, 5, 5);
  context.fill();
  update();
}

function update(){
  if(ps){
    playerShoot();
  }
  requestAnimationFrame(update);
}

function playerShoot(){
    var b = new bullet(2);
}

function bullet(speed){
    this.speed = speed;
  speed++;
  context.ellipse((cw-1)/2, ch-10-speed, 1, 3, 0, 0, Math.PI*2);
  context.fill();
}

document.addEventListener("keydown", function(e){
  switch(e.keyCode){
    case 32:
        ps = true;
      break;
  };
});

document.addEventListener("keyup", function(e){
  switch(e.keyCode){
    case 32:
        ps = false;
      break;
  };
});

1 个答案:

答案 0 :(得分:0)

我在代码本身的注释中解释了很多代码。

其他几点:

  • 某些浏览器(包括我的,即Firefox v44.0.2)不会绘制省略号。所以我把你的子弹做了另一个矩形。
  • 我使用fillRect代替rect,因为我知道的更好。
  • 我用不透明的背景颜色画过旧的子弹。但是,如果需要,您还可以清除上一个项目符号周围的矩形。
  • 您的示例中增加了速度。即使你已经得到了你想要的视觉效果,从概念的角度来看,这可能不是你想要的。我怀疑你希望你的子弹以恒定的速度移动。因此,speed变量应该是常数,即不变。相反,您应该使用speed常量来定期更改项目符号的位置。我改变了bulletY,这是子弹的垂直位置。
  • 为简单起见,我一次只允许屏幕上有一颗子弹。
  • 我将代码限制为运行500个周期。这主要是为了不惹恼尝试代码的Stack Overflow用户......他们不希望发生无限循环。



window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var cw = canvas.width;
var ch = canvas.height;
var ps = false;

// some new variables
var bulletShowing = false; // is a bullet currently showing?
var bulletY; // the vertical position of the bullet
var speed = 8; // the bullet speed
var time = 500; // the time remaining

init();

function init() {
  
  // draw background
  context.fillStyle = "yellow";
  context.fillRect(0, 0, cw, ch);
  
  // draw gun
  context.fillStyle = "black";
  context.fillRect((cw - 5) / 2, ch - 5, 5, 5);

  // update the scene
  update();
}

function update() {
  if (ps) {
    playerShoot();
  }
  
  // if a bullet is supposed to be showing then, well, show it
  if (bulletShowing) {
    
    // redraw the bullet (erase the old, draw the new)
    drawBullet();
    
    // if the bullet has gone off-screen, allow a new shot
    if (bulletY < -5) {
      bulletShowing = false;
    }
  }
  
  // give a visual indicator of time remaining
  document.querySelector("div").innerHTML = "Time: " + time;
  
  // decrement the time
  time -= 1;
  
  // if there is still time remaining, do it all again
  if (time >= 0) {
    requestAnimationFrame(update);
  }
}

function playerShoot() {

  // indicate a bullet will now be showing
  bulletShowing = true;

  // start the bullet out near the gun
  bulletY = ch - 10;
}

function drawBullet() {
  
  // erase the old bullet by drawing over it with the background color
  // this rectangle is slightly larger than the bullet itself
  // to ensure the entire old bullet is drawn over
  context.fillStyle = "yellow";
  context.fillRect((cw - 1) / 2 - 2, bulletY - 1, 5, 7);
  
  // move the bullet position
  bulletY -= speed;
  
  // draw the new bullet
  context.fillStyle = "black";
  context.fillRect((cw - 1) / 2 - 1, bulletY, 3, 5);
}

document.addEventListener("keydown", function(e) {
  switch (e.keyCode) {
    case 32:
      
      // only allow one bullet on the screen at a time
      // (for the sake of coding simplicity)
      if (!bulletShowing) {
        ps = true;
      }
      break;
  };
});

document.addEventListener("keyup", function(e) {
  switch (e.keyCode) {
    case 32:
      ps = false;
      break;
  };
});
&#13;
#myCanvas {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, 5%);
  background-color: #cccccc;
  z-index: -1;
}
&#13;
<p>Click on the canvas, then use the space bar to fire bullets one at a time.</p>
<div></div>
<canvas id="myCanvas" width=300 height=150></canvas>
&#13;
&#13;
&#13;