0

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

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;
  };
});
4

1 回答 1

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;
  };
});
#myCanvas {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, 5%);
  background-color: #cccccc;
  z-index: -1;
}
<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>

于 2016-03-02T08:34:55.050 回答