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 > stageRef.stageHeight)
removeSelf();
//if (y - 15 < target.y && y + 15 > 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);
}
}
}