# Fighting Game AI (Code Needed)

**URL:** <https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682>\
**Category:** programming\
**Created:** [March 20, 2006, 6:18pm UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682 "2006-03-20T18:18:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ravmaster](https://avatars.discourse-cdn.com/v4/letter/r/ee59a6/32.png) [@Ravmaster](https://forum.kirupa.com/u/Ravmaster)\
**Post date:** [March 20, 2006, 6:18pm UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/1 "2006-03-20T18:18:10Z")

</div>

ok im making a fighting game i just need an enemy code i need this stuff in the code…

IF it is standing stop on frame 1  
IF it is walking stop on frame 2  
If it is CLOSE to player stop on either frame 3 or 4  
IF hp is under 0 then stop on frame 5

i hope i can learn something from this code aswell 😃

thnx in advance

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [March 20, 2006, 10:31pm UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/2 "2006-03-20T22:31:13Z")

</div>

```auto

monster.onLoad=function(){

var standing:Boolean=true
var walking:Boolean=false
var hp:Number=100

}

monster.onEnterFrame=function(){
    var CloseToPlayer:Boolean=(Math.abs(this._x-hero._x)<15)
    if(standing){
       gotoAndStop(1)
    }
    if(walking){
       gotoAndStop(2)
    }
    if(CloseToPlayer){
      //make sure it only has one 3 or 4 or you will get errors
       gotoAndStop(3 or 4)
    }
    if(hp<0){
       gotoAndStop(5)
    }
}

```

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 21, 2006, 2:45am UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/3 "2006-03-21T02:45:16Z")

</div>

> [@bombsledder](#):
>
> ```auto
> monster.onLoad = function() {
> var standing:Boolean = true;
> var walking:Boolean = false;
> var hp:Number = 100;
> };
> 
> ```

That code doesn’t do anything. For starters, the onLoad event is only broadcast to movie clips with a different class association which I’m assuming the monster movie clip doesn’t have. And even if it did, variables declared with the _var_ statement inside functions are local to that functions activation object and are deleted after the function has executed :hoser:

---

<div class="post-metadata">

**Author:** ![freeskier89](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@freeskier89](https://forum.kirupa.com/u/freeskier89)\
**Post date:** [March 21, 2006, 3:00am UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/4 "2006-03-21T03:00:15Z")

</div>

In other words the code should be changed to:

```auto

monster.standing=true
monster.walking=false
monster.hp=100
monster.onEnterFrame=function(){
    var CloseToPlayer:Boolean=(Math.abs(this._x-hero._x)<15)
    if(this.standing){
       this.gotoAndStop(1);
    }
    if(this.walking){
       this.gotoAndStop(2);
    }
    if(CloseToPlayer){
      //make sure it only has one 3 or 4 or you will get errors
      this.gotoAndStop(3 or 4)
    }
    if(this.hp<0){
       this.gotoAndStop(5)
    }
}

```

:thumb:

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [March 21, 2006, 3:06am UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/5 "2006-03-21T03:06:50Z")

</div>

don’t matter i wasn’t thinking about that possibility, i put this code for him to have to re-make anyways

---

<div class="post-metadata">

**Author:** ![freeskier89](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@freeskier89](https://forum.kirupa.com/u/freeskier89)\
**Post date:** [March 21, 2006, 3:10am UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/6 "2006-03-21T03:10:41Z")

</div>

True, but if he is just learning AS and he is given the wrong information, it would most likely lead him to frustation over why his code isn’t working.

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [March 21, 2006, 3:43am UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/7 "2006-03-21T03:43:45Z")

</div>

ok, like i said i was thinking and wrote the code like as1 in as2, plus thats what the forums are for he could just post and say it doesn’t work and i probably would have looked at his .fla and been like ok my mistake 😮

PSSSS… I need a cooler footer and icon thing

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [March 21, 2006, 10:36am UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/8 "2006-03-21T10:36:55Z")

</div>

:lol: Way to hack poor old Bomby there

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [March 22, 2006, 2:07am UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/9 "2006-03-22T02:07:38Z")

</div>

lol, that was not my day lol =(

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [March 22, 2006, 2:50am UTC](https://forum.kirupa.com/t/fighting-game-ai-code-needed/182682/10 "2006-03-22T02:50:09Z")

</div>

Why always recalculate closeToPlayer?

**Edit** : Wasn’t thinking. You have to do it anyways. I thought you were using else-if’s… until I looked. 😛

```auto
monster.standing=true
monster.walking=false
monster.hp=100

monster.onEnterFrame=function(){
    if(this.standing){
       this.gotoAndStop(1);
    }
    if(this.walking){
       this.gotoAndStop(2);
    }
    if(Math.abs(this._x-hero._x)<15){
      //make sure it only has one 3 or 4 or you will get errors
      this.gotoAndStop(3 or 4)
    }
    if(this.hp<0){
       this.gotoAndStop(5)
    }
}

```
