【问题标题】:How to constantly generate a moving shape with Javascript and Canvas如何使用 Javascript 和 Canvas 不断生成移动的形状
【发布时间】:2019-04-15 19:34:18
【问题描述】:

我目前正在为我的顶点项目开发一个小游戏。在游戏中,用户会尽量避免随机大小的矩形以设定的速度从屏幕右侧移动到左侧。

它是使用面向对象的 Javascript 构建的,并且我已经为它分配了一个匿名函数,但是,我似乎无法让它生成一个形状并为其设置动画,而不是在函数被调用的初始时间。如果我创建多个对象,这个问题可以解决,但我希望这个函数自动运行并生成不仅仅是第一个矩形。

我尝试使用间隔调用该函数,以强制它重新运行该函数而没有结果。我还尝试分离初始化函数以使用参数调用它以生成赋予它的形状数量。

该函数通过初始调用生成形状,并确定颜色、大小和位置,并将其绘制在画布上。

var randomRectangle = function(){
this.init = function() {
this.speed = 4;
this.x = canvas.width-50;
this.y = Math.floor(Math.random()*280) + 40;
this.w = Math.floor(Math.random()*200) + 50;
this.h = Math.floor(Math.random()*150) + 20;
this.col = "#b5e61d";
}
this.move = function(){
this.x -= this.speed;
}

this.draw = function(num){
draw.rectangles(this.x, this.y, this.w, this.h, this.col);
}
};

这是初始化对象的地方,循环在画布上生成所有对象和动画。

randRecs = new randomRectangle();
randRecs.init();

function loop(){
draw.clear();

player.draw();
player.move();

wall1.draw();
wall2.draw();

randRecs.draw();
randRecs.move();


}


var handle = setInterval(loop, 30);

我希望矩形在新的 y 坐标处以新的大小连续生成,然后从屏幕的右侧移动到左侧。但是,只有一个矩形被创建和动画化。

【问题讨论】:

  • 您需要列出您想要绘制的矩形列表,并不断将新的矩形推送到该列表中。
  • 你能澄清一下我该怎么做吗?我试过这个:function rectangles(){ var y = Math.floor(Math.random()*280) + 40; var w = Math.floor(Math.random()*200) + 50; var h = Math.floor(Math.random()*150) + 20; console.log(y, w, h); draw.rectangles(canvas.width, y, w, h); } 以此作为让它不断运行的一种方式:var rectangleGen = setInterval(rectangles, 2000); 但没有结果。这些数字每 2 秒生成一次,似乎它们没有绘制
  • 查看下面答案中的代码,如果您需要任何额外帮助,请联系我!

标签: javascript html5-canvas


【解决方案1】:

var list = [];
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');

var randomRectangle = function() {
  this.init = function() {
    this.speed = 4;
    this.x = canvas.width - 50;
    this.y = Math.floor(Math.random() * 280) + 40;
    this.w = Math.floor(Math.random() * 200) + 50;
    this.h = Math.floor(Math.random() * 150) + 20;
    this.col = "#b5e61d";
  }
  this.move = function() {
    this.x -= this.speed;
    // restart x position to reuse rectangles
    // you can change the y value here to a new random value
    // or you can just remove with array.splice
    if (this.x < -50) this.x = canvas.width - 50;
  }

  this.draw = function(num) {
    draw.rectangles(this.x, this.y, this.w, this.h, this.col);
  }
};

function loop() {
  draw.clear();

  //player.draw();
  //player.move();

  //wall1.draw();
  //wall2.draw();

  // call the methods draw and move for each rectangle on the list
  for (var i=0; i<list.length; i++) {
    rec = list[i];
    rec.draw();
    rec.move();
  }
}

// spawn any number of new rects in a specific interval
var rectsPerSpawn = 1;
function addRects() {
  for (var i=0; i<rectsPerSpawn; i++) {
    if (list.length < 100) {
      var rec = new randomRectangle();
      list.push(rec);
      rec.init();
    }
  }
}
// every half second will spawn a new rect
var spawn = setInterval(addRects, 500);

var draw = {
  clear: function () {
    ctx.clearRect(0,0,canvas.width,canvas.height);
  },
  rectangles: function (x, y, w, h, col) {
    ctx.fillStyle = col;
    ctx.fillRect(x,y,w,h);
  }
}

var handle = setInterval(loop, 30);
&lt;canvas&gt;&lt;/canvas&gt;

【讨论】:

  • Uncaught SyntaxError: Unexpected token } ;)
  • 抱歉,我正在编辑以限制列表中的项目数!
猜你喜欢
  • 2017-01-04
  • 1970-01-01
  • 2023-03-02
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多