I am encountering a problem with the movement of the space invaders. Specifically, the if (forward) loop in the drawEnemies function. There are also no errors in the console. Can you also give me any ideas on how to improve my code? I have also included the alien function which draws them in case it might have something to do with that.
Here is a snippet
function alien(x,y,w,h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.draw = function() {
context.drawImage(enemy, this.x, this.y, this.w, this.h)
}
}
function drawEnemies() {
var dx = 0;
var dy = 0;
var forward = true;
var changeShape = setInterval(drawEnemies, 500);
if (enemies.length != 55) {
for (var i = 0; i < 11; i++) {
var enemyOne = new alien(i*65+dx,50+dy,40,40);
enemiesRow1.push(enemyOne);
enemies.push(enemyOne);
enemiesRow1[i].draw();
}
if (enemies.length = 11) {
for (var i = 0; i < 11; i++) {
var enemyTwo = new alien(i*65+dx,100+dy,40,40);
enemiesRow2.push(enemyTwo);
enemies.push(enemyTwo);
enemiesRow2[i].draw();
}
}
if (enemies.length = 22) {
for (var i = 0; i < 11; i++) {
var enemyThree = new alien(i*65+dx,150+dy,40,40);
enemiesRow3.push(enemyThree);
enemies.push(enemyThree);
enemiesRow3[i].draw();
}
}
if (enemies.length = 33) {
for (var i = 0; i < 11; i++) {
var enemyThree = new alien(i*65+dx,150+dy,40,40);
enemiesRow3.push(enemyThree);
enemies.push(enemyThree);
enemiesRow3[i].draw();
}
}
if (enemies.length = 44) {
for (var i = 0; i < 11; i++) {
var enemyFour = new alien(i*65+dx,200+dy,40,40);
enemiesRow4.push(enemyFour);
enemies.push(enemyFour);
enemiesRow4[i].draw();
}
}
if (enemies.length == 55) {
for (var i = 0; i < 11; i++) {
var enemyFive = new alien(i*65+dx,250+dy,40,40);
enemiesRow5.push(enemyFive);
enemies.push(enemyFive);
enemiesRow5[i].draw();
}
}
context.clearRect(0, 0, canvas.width, canvas.height);
if (forward) {
dx += 20;
if (dx > 500) {
forward = false;
}
} else {
dx -= 20;
if (dx < 20) {
forward = true;
dy += 20;
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire