I would really appreciate if someone would help me with this code I made. It’s for the enemies in the game.
What it does is that it follows the player, in a 4 way direction (Much like Zelda: A link to the past). ** Here’s the code:**
[font="]
onClipEvent (load) {
movespeed = 5;
}
onClipEvent (enterFrame) {
// LEFT
if (this._x<_root.player._x) {
this._x += movespeed;
this.gotoAndStop("rwalk");
}
// RIGHT
if (this._x>_root.player._x) {
this._x -= movespeed;
this.gotoAndStop("lwalk");
}
// UP
if (this._y<_root.player._y) {
this._y += movespeed;
this.gotoAndStop("fwalk");
}
// DOWN
if (this._y>_root.player._y) {
this._y -= movespeed;
this.gotoAndStop("bwalk");
}
}
[/font]<o:p>
</o:p>
However, as you can see I’ve added lines where the enemy changes his animation/direction to where he should face. So when the Player is above the enemy, for example, the enemy should change his animation so that he face the player.
This doesn’t work though, I’ve tried many ways to fix it but it just leads to futile. The enemy just does the **UP & DOWN **animations (bwalk & fwalk). Not for LEFT & RIGHT (rwalk & lwalk). He still walks towards the player though, no matter what _x & _y value. Just the animation that tends to mess up.
May seem a bit fuzzy, sorry for that, but if anyone can help me out with this… much thanks to you.
If you need an online example, I could upload one on imageshack or something.
The problem is happening due to the order of preference the statements are being executed in, its almost bedtime here, so i can’t do anything at the moment, but if it isn’t solved by tomorrow, i will fix it for you!
And it would be great if you can attach your mini-fla here, so that i can fix it!
EDIT:- I made this short code for you before going to bed, and it will work ok, test it out!!
onClipEvent(load)
{
var movespeed=2;
}
onClipEvent(enterFrame)
{
var distx=Math.round(_root.player._x-this._x);
var disty=Math.round(_root.player._y-this._y);
if(distx<0 and Math.abs(disty)<=Math.abs(distx))
{
this.gotoAndStop("left");
if(Math.abs(distx)>movespeed)
this._x-=movespeed;
}
else if(distx>0 and Math.abs(disty)<=Math.abs(distx))
{
this.gotoAndStop("right");
if(Math.abs(distx)>movespeed)
this._x+=movespeed;
}
if(disty<0 and Math.abs(distx)<=Math.abs(disty))
{
this.gotoAndStop("up");
if(Math.abs(disty)>movespeed)
this._y-=movespeed;
}
else if(disty>0 and Math.abs(distx)<=Math.abs(disty))
{
this.gotoAndStop("down");
if(Math.abs(disty)>movespeed)
this._y+=movespeed;
}
}
onClipEvent(load)
{
var movespeed=2;
}
onClipEvent(enterFrame)
{
// calculate angle
var distx = Math.round(_root.player._x - this._x);
var disty = Math.round(_root.player._y - this._y);
var theta = Math.atan2(disty, distx);
// move that direction
var deltax = Math.cos(theta) * movespeed;
var deltay = Math.sin(theta) * movespeed;
this._x += deltax;
this._y += deltay;
// figure out what frame to go to
// i might have these '>' and '<' wrong, experiment with them until they work
if (deltay > deltax && deltay > -deltax) this.gotoAndStop("down");
if (deltay > -deltax && deltay < deltax) this.gotoAndStop("right");
if (deltay < deltax && deltay < -deltax) this.gotoAndStop("up");
if (deltay < -deltax && deltay > deltax) this.gotoAndStop("left");
}