If you're using a generic Sprite instance for creating instance of enemy, you're limiting what they'll be able to do (not to mention making your code unnecessarily complex). So if you're not already doing so I'd recommend making the enemy a class of its own...something like this (note that you'll have to update the package, and maybe the class name if you can't use or don't like "Enemy"):
[source]
package your.package.path {
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.events.Event;
public class Enemy extends Sprite {
private var _excArea:Rectangle;
public function Enemy(spawnExclusionArea:Rectangle) {
this._excArea=spawnExclusionArea;
//Ensure that enemy is on the stage before trying to update its position...
this.addEventListener(Event.ADDED_TO_STAGE, this.placeEnemy);
}
private function placeEnemy(eventObj:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, this.placeEnemy);
this.x=this.getExcludedPoint("x");
this.y=this.getExcludedPoint("y");
}
function getExcludedPoint(axis:String):Number {
if (Math.random()<=0.5) {
if (axis=="x") {
//get point left of defined area...
return (Math.random()*(_excArea.x-this.width));
} else {
//get point above defined area...
return (Math.random()*(_excArea.y-this.height));
}
} else {
if (axis=="x") {
//get point to right of defined area...
return ((_excArea.width+_excArea.x)+(Math.random()*(stage.stageWidth-(_excArea.width+_excArea.x))));
} else {
//get point below defined area...
return ((_excArea.height+_excArea.y)+(Math.random()*(stage.stageHeight-(_excArea.height+_excArea.y))));
}
}
}
}
[/source]
Using an Enemy class will allow you to extend your code to create custom enemies (with custom behaviours, graphics, etc.):
[source]
package your.package.path {
import your.package.path.Enemy;
public class Boss extends Enemy {
...
[/source]
Note that in the exclusion area calculations we have to use stageWidth and stageHeight instead of the stage's width/height properties (these tell us the dimensions of all of the content on the stage, which typically aren't the dimensions of the whole stage area). If you want a larger or smaller display area (for example, your game doesn't take up the entire stage), change the stageWidth and stageHeight values to your maximum viewable area.
In the class that spawns your enemies, creating a randomly placed enemy is very simple:
[source]
import your.package.path.Enemy;
public function createEnemy():Enemy {
var exclusionArea:Rectangle=new Rectangle (100,100,300,200); //Don't generate enemies in a rectangle 300 wide by 200 high at position 100x100
var newEnemy:Enemy=new Enemy(exclusionArea);
this.addChild(newEnemy);
return (newEnemy);
}
[/source]
Because the random placement is done in the Enemy class constructor, this is literally all that's needed. The createEnemy function returns the new Enemy instance, but that's entirely optional.
If you wanted to keep track of all active enemies, you could extend the Enemy class to track itself using a static variable:
[source]
public class Enemy extends Sprite {
private static var _enemies:Vector.<Enemy>=new Vector.<Enemy>();
private var _excArea:Rectangle;
public function Enemy(spawnExclusionArea:Rectangle) {
this._excArea=spawnExclusionArea;
//Ensure that enemy is on the stage before trying to update its position...
this.addEventListener(Event.ADDED_TO_STAGE, this.placeEnemy);
this.addEventListener(Event.REMOVED_FROM_STAGE, this.removeEnemy);
}
private function placeEnemy(eventObj:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, this.placeEnemy);
this.x=this.getExcludedPoint("x");
this.y=this.getExcludedPoint("y");
this.addEnemyToList();
}
private function removeEnemy(eventObj:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, this.removeEnemy);
this.removeEnemyFromList();
}
private function addEnemyToList():void {
_enemies.push(this);
}
private function removeEnemyFromList():void {
var compactVector:Vector.<Enemy>=new Vector.<Enemy>();
for (var count:uint=0; count<_enemies.length; count++) {
var currentEnemy:Enemy=_enemies[count];
if (currentEnemy!=this) {
compactVector.push(currentEnemy)
}
}
_enemies=compactVector;
}
public static function get enemies():Vector.<Enemy> {
return (_enemies);
}
function getExcludedPoint(axis:String):Number {
if (Math.random()<=0.5) {
if (axis=="x") {
//get point left of defined area...
return (Math.random()*(_excArea.x-this.width));
} else {
//get point above defined area...
return (Math.random()*(_excArea.y-this.height));
}
} else {
if (axis=="x") {
//get point to right of defined area...
return ((_excArea.width+_excArea.x)+(Math.random()*(stage.stageWidth-(_excArea.width+_excArea.x))));
} else {
//get point below defined area...
return ((_excArea.height+_excArea.y)+(Math.random()*(stage.stageHeight-(_excArea.height+_excArea.y))));
}
}
}
}
[/source]
From outside the Enemy class, all currently active enemies would be available as Enemy.enemies (a vector array). I use a Vector since it's more efficient, but making _enemies a standard Array instance would work equally well.
I've added the list management to happen automatically -- a new Enemy is added when added to the stage display list, and it's automatically removed when removed from the stage. If you wanted to maintain the list manually (for example, an Enemy isn't considered to be a part of the list until something else happens), just make the relevant functions public and call them from outside the Enemy class.
Hope this helps.