40 isFollow - JcerelusDev/CanvasGameJs GitHub Wiki
What is isFollow ?
isFollow is a method that allows enemies and boss to follow the player if the player is in their range.
How to use isFollow ?
there are 2 ways possible to use that method .
The first one is to allow each enemy to follow the player if he is close to them.
And the second is to let a boss following the player if he is close to him.
There are two special variables :
goLeft and goRight
They are two boolean variables
You are going to check each of them,then write your logic inside of each of them.
considering we have an array of enemies
we need to loop for each enemy
/* in the update method do
the following or you can use
another function in order
to keep your code clean
and then call that function
in the update method.
*/
game.update = function(){
//code example
for(var ene of enemies){
if(isFollow(player,ene)){
if(goLeft){
ene.x-= 1
// in case of a spritesheet
ene.row = //to the index that looks to the left
ene.col++
if(ene.col >= to the number of columns){
ene.col = 0
}
}
if(goRight){
ene.x+= 1
// in case of a spritesheet
ene.row = //to the index that looks to the right.
ene.col++
if(ene.col >= to the number of columns){
ene.col = 0
}
}
}
}
}
considering we want to apply it for a boss
/* in the update method do
the following or you can use
another function in order
to keep your code clean
and then call that function
in the update method.
*/
game.update = function(){
//code example
if(isFollow(player,box)){
if(goLeft){
box.x-=1
// in case of a spritesheet
ene.row = //to the index that looks to the left
ene.col++
if(ene.col >= to the number of columns){
ene.col = 0
}
}
if(goRight){
box.x+=1
// in case of a spritesheet
ene.row = //to the index that looks to the right
ene.col++
if(ene.col >= to the number of columns){
ene.col = 0
}
}
}
}
For the time being isFollow only works on X axis. Meaning the enemy can only follow you from left to right and from right to left.