PDA

View Full Version : My aliens will leave the stage!!


supernaut1979
07-13-2009, 08:21 PM
Help....This has been bugging me all weekeend and Im still no closer to sorting it. Im trying my hardest to make a space invaders type game using AS3. Ive got everythin nailed like scores, character movement, hitTest and collisions etc.......

The problem is with my aliens. Ive generated them on screen using using 2 for loops and an array to contain references to them, and place them all inside a sprite display container, because I figure its easier to have one eventlistener move the aliens movieclip instead of attaching an eventlistner to every instance of the alien movielip.

All works fine moving them across the screen and not leaving the stage, until I start to shoot them. When an entire column of aliens has been removed from the container the container clip starts to go beyond the bounds of the stage.

How do i detect which aliens have been removed from the alien array(what position they held in the attack wave) so that I can somehow resize the container movieclip. Or am i barking up the wrong tree??

Here is my code for placing the aliens on stage:
private function makeEnemies ():void {


for (var i:uint = 0; i < numEnemyX; i++) {
for (var j:uint = 0; j < numEnemyY; j++) {
alienEnemy = new Enemy(stage);
alienEnemy.x = i * 35 - 192.5/*+ stage.stageWidth / 2 - 177.5*/;
alienEnemy.y = j * 35 /*+ 50*/;
enemyContainer.addChild (alienEnemy);

//Add movement to each instance of Enemy
enemyContainer.addEventListener(Event.ENTER_FRAME, moveEnemy);

//Add each instance of Enemy created by for loops to enemyList array
enemyList.push (alienEnemy);
}
}

}

private function moveEnemy(evt:Event):void{
evt.target.x += enemySpeedX;

if (evt.target.x > stage.stageWidth - enemyContainer.width / 2 ){
enemySpeedX *= -1;
}else if(evt.target.x < 0 + enemyContainer.width / 2){
enemySpeedX *= -1;
}

}

rrh
07-13-2009, 08:50 PM
I'd probably store some kind of position info within the Enemy class, like so:
alienEnemy.row=j;
alienEnemy.col=i;

Then maybe store the enemyList as an array of arrays. You didn't show your code for removing the aliens when they got shot, so I can't say a lot.

supernaut1979
07-13-2009, 10:02 PM
This is the code in the enemy class that im using to remove them from the stage and the enemyList array

public function takeHit ():void {

this.gotoAndPlay ("Hit");
this.addEventListener (Event.ENTER_FRAME, removeAlien);
}

private function removeAlien (evt:Event):void {

if (this.currentFrame == 10) {
//Check enemyContainer to see if this instance of Enemy is contained inside and remove it if it is
if (Engine.enemyContainer.contains(this)) {
Engine.enemyContainer.removeChild (this);
}

this.removeEventListener (Event.ENTER_FRAME, removeAlien);

//Remove this instance of Enemy from enemyList array
Engine.enemyList.splice (Engine.enemyList.indexOf(this),1);
}

}

bluemagica
07-14-2009, 12:47 AM
well your problem is happening because
1)Child cooordinates are relative to parent coordinate.
2) A clips size (length, width) depends on it's contents position!

What you are doing is kindof wrong for hardcore OOPers, but......oh well.... to solve it, store the enemyContatiner.width in a variable at the begining , right after it is filled with enemies....later on in the movement function, just reference this!

supernaut1979
07-14-2009, 08:06 AM
Thanks for the reply. Will have a play and see if I can sort it. Any other suggestions welcome.

Kind of new to OOP anyway with coming from AS2, but dont suppose you can point out wot it is that I have done that "hardcore OOPers" wouldnt have done.....

Thanks again :)

rrh
07-14-2009, 03:48 PM
Hardcore OOPers would probably have put more of that "list removal" logic inside the Engine class, so tihs function would be more like:
private function removeAlien (evt:Event):void {
if (this.currentFrame == 10)
Engine.removeEnemy(this);
//or if you were really hardcore:
if (this.currentFrame == 10)
dispatchEvent(new Event(MyGameEvents.ENEMY_REMOVAL));


At least you are referencing Engine, rather than just going parent.parent

rrh
07-14-2009, 04:05 PM
One thing noteworthy is right now, you are testing from the centre of enemyContainer an equal distance on both sides, using enemyContainer.width / 2

If you shoot the right-most column, that should reduce the right-hand test by one column width, and have no change to the left-hand test. But the way you are implementing it now, it will reduce the distance of both by half a column width.

So at the very least, you should get Engine or the enemyContainer to keep a list of how many aliens are left in each column, and then when testing the position of the enemyContainer, test a distance determined by the number of empty columns on the right or left side.

rrh
07-15-2009, 03:43 PM
I just remembered, there might be a simpler way. Look up getBounds() you could use that to find the left and right side of your enemyContainer.