# Expert help on ai needed

**URL:** https://forum.kirupa.com/t/expert-help-on-ai-needed/174588
**Category:** programming
**Created:** [January 6, 2006, 11:55pm UTC](https://forum.kirupa.com/t/expert-help-on-ai-needed/174588 "2006-01-06T23:55:51Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![mayjarboi](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@mayjarboi](https://forum.kirupa.com/u/mayjarboi)
#### Post date: [January 6, 2006, 11:55pm UTC](https://forum.kirupa.com/t/expert-help-on-ai-needed/174588/1 "2006-01-06T23:55:51Z")

</div>

here is the code for my enemy

> Dir = -1;  
> Action = “Stand”;  
> Direction = “Left”;  
> Timer = 20;  
> Attack = false;  
> Attacked = false;  
> this.onEnterFrame = function() {  
> gotoAndStop("Regular “+Action+” "+Direction);  
> var Distance = \_root.Hero.\_x-\_x;  
> //-----------------------------------------//  
> if (Distance\>0) {  
> Direction = “Right”;  
> Dir = 1;  
> } else {  
> Direction = “Left”;  
> Dir = -1;  
> }  
> //-----------------------------------------//  
> if (\_root.Hero.Action == “Attack” && Math.abs(Distance)\<=\_width) {  
> Action = “Block”;  
> } else if (Math.abs(Distance)\<200) {  
> if (Attacked) {  
> Action = “Walk”;  
> \_x += -Dir_2.5;  
> Timer -= .5;  
> if (Timer == 0) {  
> Timer = 25;  
> Attacked = false;  
> }  
> } else {  
> if (Math.abs(Distance)\>\_width) {  
> \_x += Dir_2.5;  
> Action = “Walk”;  
> } else if (Math.abs(Distance)\<=\_width) {  
> if (Attacked == false) {  
> Action = “Attack”;  
> } else if (Attacked == true&&Math.abs(Distance)\<=\_width-6) {  
> Action = “Block”;  
> }  
> }  
> }  
> } else if (Math.abs(Distance)\<200) {  
> Action = “Stand”;  
> }  
> };

there is a slight problem, i want the enemy to finish attacking once he has started, even when the hero moves away.
