1 decade ago
by Mo3
Is there any way to make CPU's chase the player?
1 decade ago
by woyteck
The algorithm is called A*, it works well in 2D games, dunno if it'll be any good for a platform game though.
I learned about it from here:
http://bostongamejams.com/akihabara-tutorials/akihabara-tutorial-part-5-enemy-ai
when I experimented with another framework.
1 decade ago
by Mo3
Thank you!
I'm pretty new to coding thus I'm not sure how to do this properly.
I got this in my enemy.js entity:
update: function() {
var player = this.getEntitiesByType( EntityPlayer )[0];
if( player ) {
//What here?
}
var xdir = this.flip ? -1 : 1;
this.vel.x = this.speed * xdir;
this.parent();
},
I think I have to use update, but what should I put inside the if-brackets?
1 decade ago
by dominic
You could get the angle to your target entity (the player in this case) and set the velocity of the entity to move torwards it:
var angle = this.angleTo( player );
this.vel.x = Math.cos(angle) * this.speed;
this.vel.y = Math.sin(angle) * this.speed;
Please try to state your questions a bit more explicit in the future. Guessing what you want to achieve is no fun :)
1 decade ago
by Mo3
Thank you!
update: function() {
var player = this.getEntitiesByType( EntityPlayer )[0];
var angle = this.angleTo( player );
this.vel.x = Math.cos(angle) * this.speed;
this.vel.y = Math.sin(angle) * this.speed;
this.parent();
},
This is what I have right now, yet the game just stops loading at 90%, without any error message/etc.. Where could be the problem?
1 decade ago
by wicht
There should be an error in your console if it doesn't load completely.
Hard to say what's wrong if you only present a short snippet of source.
Two wild guesses:
+ The dependency to EntityPlayer is not defined (see requires method)
+ The player hasn't spawned yet. Trying to develop defensive is always a good idea:
update: function() {
var player = this.getEntitiesByType( EntityPlayer )[0];
if (player) {
var angle = this.angleTo( player );
this.vel.x = Math.cos(angle) * this.speed;
this.vel.y = Math.sin(angle) * this.speed;
}
this.parent();
},
But it hasn't to be one of that...
1 decade ago
by wicht
Btw: The getEntitiesByType method is a member of the Game Object ... won't work on an Entity...
var player = ig.game.getEntitiesByType( EntityPlayer )[0];
Just use the Devtools with the console ... all of that above will show errors in the console.
Page 1 of 1
« first
« previous
next ›
last »