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;
}
}
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;
}
}