私はJavascriptのゲームにはかなり新しいので、分かりやすい質問があれば気にしないでください。 私はフロッグゲームに取り組もうとしています。このために私はオブジェクトを持っています。そして、新しいコンストラクタを一貫して作成したいので、バグがたくさんあるように見えるはずです。Javascript game自動的に新しいオブジェクトを作成して配列に連続的に追加する[javascript]
これは私の敵のオブジェクトです。
// Enemies our player must avoid
var Enemy = function(x,y) {
// Variables applied to each of our instances go here,
// we've provided one for you to get started
// The image/sprite for our enemies, this uses
// a helper we've provided to easily load images
this.sprite = 'images/enemy-bug.png';
this.x = x;
this.y =y;
};
// Update the enemy's position, required method for game
// Parameter: dt, a time delta between ticks
Enemy.prototype.update = function(dt) {
// You should multiply any movement by the dt parameter
// which will ensure the game runs at the same speed for
// all computers.
this.x = this.x+((Math.random() * (15 - 1 + 1) + 1)*dt*35);
this.y = this.y;
};
// Draw the enemy on the screen, required method for game
Enemy.prototype.render = function() {
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
};
し、私は私が唯一のバグの単一の列を見ることができる配列
// Place all enemy objects in an array called allEnemies
var allEnemies=[];
allEnemies.push(new Enemy(0,135))
allEnemies.push(new Enemy(0,225))
allEnemies.push(new Enemy(0,50))
に手動でそれらを押してください。私はこれを上記のシナリオが自動的に起こるようにしたい、私はここでコール関数を使用する必要があると思ったが、それでも私は好む連続的な間隔で自動的にそれを行う必要があります。
連続した間隔で起こるようにするにはsetIntervalを使用できます – Geeky