# Enemy AI help

**URL:** <https://forum.kirupa.com/t/enemy-ai-help/203870>\
**Category:** programming\
**Created:** [October 15, 2006, 4:07am UTC](https://forum.kirupa.com/t/enemy-ai-help/203870 "2006-10-15T04:07:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pepin](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pepin](https://forum.kirupa.com/u/Pepin)\
**Post date:** [October 15, 2006, 4:07am UTC](https://forum.kirupa.com/t/enemy-ai-help/203870/1 "2006-10-15T04:07:44Z")

</div>

Alright I’m trying to get the enemy to come out to a certain ammount of distance and then shoot a ball at me. I’ve got this so far

On the Enemy

```auto
onClipEvent (load) {
 function reset() {
  this._x = 970;
  this._y = random(200)+100;
  this.gotoAndStop(1);
  ehits = 8;
  movepos1 = 40+random(20);
  inpos = false;
  counter = 0;
 }
 reset();
}
onClipEvent (enterFrame) {
 counter++;
 if (this._x<-148) {
  reset();
 }
 if (!inpos) {
  this._x -= 2;
  this._y -= 1;
 }
 if (movepos1 == counter) {
  movepos1 = movepos1;
  inpos = true;
 }
 if (inpos) {
  _root.attachMovie("mlaser", "mlaser"+i, i());
  i++;
 }
 if (this._currentframe == 1) {
  if (this.hitTest(_root.spaceship)) {
   this.gotoAndStop(2);
   _root.health -= 30;
   _root.shld.play();
  }
 }
 if (ehits<1) {
  this.gotoAndPlay(2);
  _root.score += 150;
  ehits = 4;
 }
}

```

Then this on a frame

```auto
_root["mlaser"+i]._x = saucer._x;
_root["mlaser"+i]._y = saucer._y;
_root["mlaser"+i].onEnterFrame = function() {
 angle = Math.atan2(mlaser._y-_root.box._y, mlaser._x-_root.box._x);
 this.dx = Math.cos(angle)*10;
 this.dy = Math.sin(angle)*10;
 this._y -= this.dy;
 this._x -= this.dx;
};

```

I’ve been trying a lot of things for that one. The problem is that the ball ends up near the axis all the time. I’m probaly doing something stupid wrong. Help is very apprectiated.

Edit: I forgot some of the code up there. But now I’ve got it to at lease go to the enemies position.
