1 decade ago by Gamma
A few months ago I asked the question of how to make the player get chased by an enemy, specifically on a sidescroller where the player is bound to x and y coordinates excluding angles whatsoever. One of the users, gave me this piece of code which worked perfectly until the player died which gave me some errors, details below. Note the users code had been modified a bit. original post can be found here: Enemy Sensing Players....
Players init function:
Enemies update function:
Now here's where the problem comes in. The enemy perfectly chases the player just fine, no errors occur at this time, but here's a list of problems that occur.
1.
After the player is killed by the enemy, the console takes out an error and freezes the game. The error states:
"Uncaught TypeError: Cannot read property 'pos' of undefined"
And the error points to the "chasing" code ( the code above in the enemies update function) specifically where it says:
2
When the player does not kill the enemy and while the enemy is still chasing the player, the player decides to go a bit further away from the enemy and as the enemy gets closer to a specific spot in which it must return it starts going crazy by staying the same spot moving back and forth rapidly, and I think it has to do with the method below:
Can anyone please assist me in figuring out how to make enemies chase a player efficiently without any errors on a sidescroller. I appreciate the help I got last time from Silverspectro. If there isn't a fix or something better, please post some code below I would appreciate it greatly. My game is almost done, this is pretty much the last aspect that I need implemented on the game that had been bugging me all week. Thank you all in advance.
Players init function:
ig.game.player = this;
Enemies update function:
var player = ig.game.getEntitiesByType( EntityPlayer )[0]; var xdir = this.flip ? -1 : 1; if ( this.distanceTo( ig.game.player) < 300 ) { var target = 0; target = player.pos.x; //ig.log ( this.target ); } if ( target < this.pos.x ) { this.flip = true; this.vel.x = this.speed * xdir; } else if ( target > this.pos.x ) { this.flip = false; this.vel.x = this.speed * xdir; } else { this.vel.x = this.speed*xdir; }
Now here's where the problem comes in. The enemy perfectly chases the player just fine, no errors occur at this time, but here's a list of problems that occur.
1.
After the player is killed by the enemy, the console takes out an error and freezes the game. The error states:
"Uncaught TypeError: Cannot read property 'pos' of undefined"
And the error points to the "chasing" code ( the code above in the enemies update function) specifically where it says:
target = player.pos.x;
2
When the player does not kill the enemy and while the enemy is still chasing the player, the player decides to go a bit further away from the enemy and as the enemy gets closer to a specific spot in which it must return it starts going crazy by staying the same spot moving back and forth rapidly, and I think it has to do with the method below:
// near an edge? return! if( !ig.game.collisionMap.getTile( this.pos.x + (this.flip ? +4 : this.size.x -4), this.pos.y + this.size.y+1 ) ) { this.flip = !this.flip; } var xdir = this.flip ? -1 : 1; this.vel.x = this.speed * xdir; this.currentAnim.flip.x = this.flip; this.parent(); },
Can anyone please assist me in figuring out how to make enemies chase a player efficiently without any errors on a sidescroller. I appreciate the help I got last time from Silverspectro. If there isn't a fix or something better, please post some code below I would appreciate it greatly. My game is almost done, this is pretty much the last aspect that I need implemented on the game that had been bugging me all week. Thank you all in advance.