# Enemy A.I help for Flash MX

**URL:** <https://forum.kirupa.com/t/enemy-a-i-help-for-flash-mx/156260>\
**Category:** programming\
**Created:** [July 24, 2005, 11:59am UTC](https://forum.kirupa.com/t/enemy-a-i-help-for-flash-mx/156260 "2005-07-24T11:59:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tokien](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Tokien](https://forum.kirupa.com/u/Tokien)\
**Post date:** [July 24, 2005, 11:59am UTC](https://forum.kirupa.com/t/enemy-a-i-help-for-flash-mx/156260/1 "2005-07-24T11:59:30Z")

</div>

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=&quot]

```

```auto

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.

Thanks in advance…

---

<div class="post-metadata">

**Author:** ![bluemagica](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bluemagica](https://forum.kirupa.com/u/bluemagica)\
**Post date:** [June 8, 2008, 6:53pm UTC](https://forum.kirupa.com/t/enemy-a-i-help-for-flash-mx/156260/2 "2008-06-08T18:53:08Z")

</div>

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!!

```auto

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;
    }
}

```

---

<div class="post-metadata">

**Author:** ![tapioca](https://avatars.discourse-cdn.com/v4/letter/t/a4c791/32.png) [@tapioca](https://forum.kirupa.com/u/tapioca)\
**Post date:** [June 9, 2008, 6:03pm UTC](https://forum.kirupa.com/t/enemy-a-i-help-for-flash-mx/156260/3 "2008-06-09T18:03:49Z")

</div>

triggggggg 🙂

```auto

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");
}

```
