首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过requestAnimationFrame从新实例中移动形状?

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

Stack Overflow用户
提问于 2016-03-02 15:06:37
回答 1查看 316关注 0票数 0

这是fiddle。当按下空格键(键码32)时,将创建一个小矩形来模拟一个项目符号。我遇到了一些问题:如何将它们移到顶部(减小y坐标)?有谁可以帮我?谢谢!

代码语言:javascript
复制
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;
  };
});
EN

回答 1

Stack Overflow用户

发布于 2016-03-02 16:34:55

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

其他几点:

  • 一些浏览器(包括我的浏览器,比如Firefoxv44.0.2)不会绘制椭圆。所以我把你的子弹改成了另一个矩形。
  • 我用fillRect代替了rect,只是因为我更了解这一点。
  • 我用不透明的背景颜色在旧的子弹上画了一遍。但是,如果您愿意,也可以清除前一个项目符号周围的矩形。
  • 您在示例中提高了速度。从概念的角度来看,这可能不是你想要的,即使你已经得到了你想要的视觉效果。我怀疑你想让你的子弹以恒定的速度移动。因此,speed变量应该是常量,也就是不变。相反,您应该使用speed常量定期更改项目符号的位置。我更改了bulletY,这是项目符号的垂直位置。
  • 为了简单起见,我一次只允许屏幕上出现一个项目符号。
  • 我将代码限制为运行500个周期。这主要是为了不惹恼尝试code...they的堆栈溢出用户,他们不希望发生无限循环。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35740598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档