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 ruimalta

Hi, I am trying to make the enemies in the level to chase the player, if he's near him, and I can only make one of each class to follow it, in the main.js I have:
just after the MyGame = ig.Game.extend({ I have:
sentinelNear: 0,
spikeNear: 0,
st: null,


in the init function I have:
var sentinelProx = 0;
var spikeProx = 0;


and in the update funtion I have:
var spike = this.getEntitiesByType( EntitySpike ) [0];
var spikeLength = this.getEntitiesByType( EntitySpike).length;

spikeProx = Math.abs( hero.pos.x - spike.pos.x );

for( var i = 0; i < spikeLength; i++ ) {
if (spikeProx < 50){
st = spike[i];
nearSpike = this.whereami(hero.pos.x, spike.pos.x);
st.flip = nearSpike;
if(nearSpike==1)
this.flip = true;//if hero is on the left side of the enemy

if(nearSpike==0)
this.flip = false;//if hero is on the right side of the enemy
}
}


the whereami funtion is:
whereami: function(xH, xE){
if( xH < xE ) //if player is on the left side of the enemy
return(1);
else (xH > xE) //if player is on the right side of the enemy
return(0);
},


So, in resume my problem is that I can make only one of the "spikes" to follow me. What can I do?

1 decade ago by ruimalta

I have made my for condition more simple:
for (var i = 0; i < numSentinels; i++) {
if (sentinelProx < 90){
enemy = this.getEntitiesByType( EntitySentinel )[i];

if( hero.pos.x < enemy.pos.x ) //if player is on the left side of the enemy
enemy.flip = true;
else (hero.pos.x > enemy.pos.x) //if player is on the right side of the enemy
enemy.flip = false;
}

}


Can anyone help me?

1 decade ago by StuartTresadern

It looks like this is in your main.js ? what i suggest you do is move it to your spike entity so that every spike you spawn has its own AI. Also look at using distanceTo
http://impactjs.com/documentation/class-reference/entity#distanceto. if you have a ref to your player entity in your main.js you can then test in your spike update:

this.distanceTo(ig.game.player)

1 decade ago by ruimalta

thanks
Page 1 of 1
« first « previous next › last »