# Making an enemy follow the player

**URL:** https://forum.kirupa.com/t/making-an-enemy-follow-the-player/309909
**Category:** flash
**Created:** [May 27, 2010, 1:54pm UTC](https://forum.kirupa.com/t/making-an-enemy-follow-the-player/309909 "2010-05-27T13:54:45Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Dammon](https://avatars.discourse-cdn.com/v4/letter/d/cab0a1/32.png) [@Dammon](https://forum.kirupa.com/u/Dammon)
#### Post date: [May 27, 2010, 1:54pm UTC](https://forum.kirupa.com/t/making-an-enemy-follow-the-player/309909/1 "2010-05-27T13:54:45Z")

</div>

I’ve been working on a game for my AS3 class that involves enemies randomly spawning from all direction and following the player in an attempt to collide with him. I can get the enemies to spawn from one direction as well as move in a single direction but I can’t get the spawning from all sides or the following the player around to work.

```
 I've been at this for a couple of weeks now and followed several suggestions and tutorials (including one from here) with no success and this thing is due by tonight :worried:. Here's the code from my enemy class. All of the code of the game is done through AS files with no coding done within movie clips or on the timeline.

```

package  
{

```
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;

public class Enemy extends MovieClip
{
    
    private var stageRef:Stage;
    private var vy:Number = 3;
    private var ay:Number = .4;
    private var target:Tank;
    
    public function Enemy(stageRef:Stage, target:Tank) : void
    {
        this.stageRef = stageRef;
        this.target = target;
        
        x = Math.random() * stageRef.stageWidth;
        y = -5;
        
        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
    }
    
    private function loop(e:Event) : void 
    {
        
        
    
        vy += ay;
        y += vy;
        
        if (y &gt; stageRef.stageHeight)
            removeSelf();
            
        //if (y - 15 &lt; target.y && y + 15 &gt; target.y)
            //fireWeapon();
    }
    
    /*private function fireWeapon() : void
    {
        stageRef.addChild(new EnemyBullet(stageRef, target, x, y, -5));
        stageRef.addChild(new EnemyBullet(stageRef, target, x, y, 5));
    }*/
    

    
    private function removeSelf() : void {
        
        removeEventListener(Event.ENTER_FRAME, loop);
        
        if (stageRef.contains(this))
            stageRef.removeChild(this);
        
    }
    
}

```

}
