1 decade ago by PressStart
Hi everyone,
I am making my first Impact JS game, a pseudo Space Invaders clone with and horizontal look (enemies advance from right to left), but i am having the following problem. I set a default "direction" for the "invaders" and move all of them in that direction till they reach the bottom or top of the screen, when i change the direction and make them advance. The problem is that when they advance, some columns of enemies "lag behind", as if they were not moved at the same time, making them disallign with the other rows. I have tried with delta time but it has not worked. What may i be doing wrong? Here is some of the code:
MyGame = ig.Game.extend({
// Create a monstersArray to contain all the spawned monsters
monstersArray: [],
// Create a monsterDirection variable to keep track of monster direction.
monstersDirection: "down",
update: function () {
// Update all entities and backgroundMaps
this.parent();
// Check if a Monster has reached bottom or top
// and set direction accordingly
for (var i = 0; i < this.monstersArray.length; i++) {
if (this.monstersArray[i].pos.y >= ig.system.height - 32) {
this.monstersDirection = "up";
} else if (this.monstersArray[i].pos.y <= 16) {
this.monstersDirection = "down";
};
};
// Move Monsters
// move monsters up and own
for (var i = 0; i < this.monstersArray.length; i++) {
if (this.monstersDirection == "down") {
this.monstersArray[i].pos.y += 4;
}
if (this.monstersDirection == "up") {
this.monstersArray[i].pos.y -= 4;
}
// make monsters advance
if (this.monstersArray[i].pos.y >= ig.system.height - 32 || this.monstersArray[i].pos.y <= 16) {
for (var i = 0; i < this.monstersArray.length; i++) {
// this.monstersSpeed -= 5;
this.monstersArray[i].pos.x -= 16;
};
}
};
this.timer = 0;
};
I am making my first Impact JS game, a pseudo Space Invaders clone with and horizontal look (enemies advance from right to left), but i am having the following problem. I set a default "direction" for the "invaders" and move all of them in that direction till they reach the bottom or top of the screen, when i change the direction and make them advance. The problem is that when they advance, some columns of enemies "lag behind", as if they were not moved at the same time, making them disallign with the other rows. I have tried with delta time but it has not worked. What may i be doing wrong? Here is some of the code:
MyGame = ig.Game.extend({
// Create a monstersArray to contain all the spawned monsters
monstersArray: [],
// Create a monsterDirection variable to keep track of monster direction.
monstersDirection: "down",
update: function () {
// Update all entities and backgroundMaps
this.parent();
// Check if a Monster has reached bottom or top
// and set direction accordingly
for (var i = 0; i < this.monstersArray.length; i++) {
if (this.monstersArray[i].pos.y >= ig.system.height - 32) {
this.monstersDirection = "up";
} else if (this.monstersArray[i].pos.y <= 16) {
this.monstersDirection = "down";
};
};
// Move Monsters
// move monsters up and own
for (var i = 0; i < this.monstersArray.length; i++) {
if (this.monstersDirection == "down") {
this.monstersArray[i].pos.y += 4;
}
if (this.monstersDirection == "up") {
this.monstersArray[i].pos.y -= 4;
}
// make monsters advance
if (this.monstersArray[i].pos.y >= ig.system.height - 32 || this.monstersArray[i].pos.y <= 16) {
for (var i = 0; i < this.monstersArray.length; i++) {
// this.monstersSpeed -= 5;
this.monstersArray[i].pos.x -= 16;
};
}
};
this.timer = 0;
};