Need help with enemy movement
- JohanL
-
JohanL
- Member since: Nov. 5, 2005
- Offline.
-
- Forum Stats
- Member
- Level 20
- Blank Slate
Hello,
me and my friend are making a platformer game, and right now I'm having some issues with an enemy code. I tried looking around the web for answers first but I never managed to find what I was looking for. :l Anyways here's the code we're using now:
onClipEvent(load) {
espeed = 3;
scale = _xscale;
active = true;
}
onClipEvent(enterFrame) {
distance = 300;
tx = this._x;
ty = this._y;
sx = _root.mcHero._x;
sy = _root.mcHero._y;
if (Math.sqrt( (sx-tx)*(sx-tx) + (sy-ty)*(sy-ty))<distance) {
if((tx < sx) && active) {
this.gotoAndStop(2);
_xscale = -scale;
this._x += espeed;
}
if((tx > sx) && active) {
this.gotoAndStop(2);
_xscale = scale;
this._x -= espeed;
}
if (Math.sqrt( (sx-tx)*(sx-tx) + (sy-ty)*(sy-ty))<distance-150) {
this.gotoAndStop(3);
espeed = 0;
}
} else {
this.gotoAndStop(1);
espeed = 3;
}
}
This is all on the enemy mc. The enemy follows the player when he is close enough, and stops to attack when he reaches the player. The problem is that when the player moves away, the enemy stands still. I want the enemy to keep following the player after an attack and if he still is in range. I've uploaded the file so you can see what I mean, I hope this all made sense. Use the W A D keys to move around.
Thanks in advance :)
- JohanL
-
JohanL
- Member since: Nov. 5, 2005
- Offline.
-
- Forum Stats
- Member
- Level 20
- Blank Slate
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
Main problem is setting espeed to zero on attack - it doesn't get reset to 3 until the hero is out of 'follow' range.
Rearranged your loops slightly with else if and also for efficiency you only need to calculate the hypotenuse once per enterFrame
onClipEvent (load) {
espeed = 3;
scale = _xscale;
active = true;
}
onClipEvent (enterFrame) {
distance = 300;
tx = this._x;
ty = this._y;
sx = _root.mcHero._x;
sy = _root.mcHero._y;
dd = Math.sqrt((sx-tx)*(sx-tx)+(sy-ty)*(sy-ty))
if (dd<distance) {
if (dd<distance-150) {
this.gotoAndStop(3);
} else if ((tx<sx) && active) {
this.gotoAndStop(2);
_xscale = -scale;
this._x += espeed;
} else if ((tx>sx) && active) {
this.gotoAndStop(2);
_xscale = scale;
this._x -= espeed;
}
} else {
this.gotoAndStop(1);
espeed = 3;
}
} - JohanL
-
JohanL
- Member since: Nov. 5, 2005
- Offline.
-
- Forum Stats
- Member
- Level 20
- Blank Slate
At 2/19/09 05:57 AM, Denvish wrote: Main problem is setting espeed to zero on attack - it doesn't get reset to 3 until the hero is out of 'follow' range.
Rearranged your loops slightly with else if and also for efficiency you only need to calculate the hypotenuse once per enterFrame
Awesome. Denvish you are my hero.


