Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

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;
};

1 decade ago by Joncom

Mind editing your post and enclosing your code between ## like this:
##
function foo() {
console.log('bar');
}
##

Result:
function foo() {
    console.log('bar');
}

Easier to read and therefore help you...

Why are your bad guys set to advance only when they are roughly outside the top and bottoms of your game?

Shouldn't advancing be tied to some sort of time interval instead?

1 decade ago by PressStart

Dear Joncom,

Thanks for the help. I am changing direction for the "bad guys" like that instead of using a time interval because they increment their speed every time they advance, so the time interval to get from one side of the screen to the other would vary. Here is 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;

};
}

};
};
Page 1 of 1
« first « previous next › last »