Hello,
I am currently learning how to use actionscript3.0. to design a space invader game which i need to carry our a study. I have manageg to maake the player shoot bullet upward. I have also populated my Aliens (50) on the screen and they can moove horizontally. Now my problem is how to make the aliens move downwards when they hit both sides of the screen. Any suggestions would be appreciated.
Conditions could be using
I think there may be problems depending on how you group or not the aliens.
The basics would be that when any alien touched the edge of the screen some all aliens are increased in height and alien (with this would lower).
if (aliensGroup.x <0) {
aliensGroup.x = 1;
aliensGroup.y + = individualAlien.height;
}
if (aliensGroup.x> aliensGroup.width) {
aliensGroup.x = aliensGroup.width-1;
aliensGroup.y + = individualAlien.height;
}
* That if the registration point is top left.
But what if they destroy the entire first column of aliens?
You would have to move all the aliens on the left
But know what aliens are on stage?
Maybe you would have to take into an array references for each alien and remove this of the array when it is destroyed
Thanks for your response but I still need some clarification.
here is what I have at the moment
var alien_direction:Number = 1.5;
var alienArray:Array = new Array();
var columns = 10;
var rows = 5;
var HspaceBetweenInvaders = 60;
var VspaceBetweenInvaders = 40;
for (var i:int=0; i<columns;i++)
{
for (var j:int=0; j<rows;j++)
{
var alien_mc:mcAlien = new mcAlien();
alien_mc.x = i * HspaceBetweenInvaders+ 55;
alien_mc.y = j* VspaceBetweenInvaders+150;
addChild(alien_mc);
alienArray.push(alien_mc);
}
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame (event:Event): void
{
for (var i:int = 0; i< alienArray.length; i++)
{
if (alienArray[i].x > stage.stageWidth ) {
alien_direction = -1.5;
}
else if (alienArray[i].x < 0 ) {
alien_direction = 1.5;
}
alienArray[i].x = alienArray[i].x + alien_direction; //move to the right
//alienArray[i].y = -alienArray[i].y
}
}
Try with this:
var alien_direction:Number = 1.5;
var alienArray:Array = new Array();
var columns = 10;
var rows = 5;
var HspaceBetweenInvaders = 60;
var VspaceBetweenInvaders = 40;
for (var i:int=0; i<columns; i++)
{
for (var j:int=0; j<rows; j++)
{
var alien_mc:mcAlien = new mcAlien();
alien_mc.x = i * HspaceBetweenInvaders + 55;
alien_mc.y = j * VspaceBetweenInvaders + 150;
addChild(alien_mc);
alienArray.push(alien_mc);
}
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
for (var i:int = 0; i< alienArray.length; i++)
{
if (alienArray[i].x > stage.stageWidth)
{
alien_direction = -1.5;
down();
}
else if (alienArray[i].x < 0 )
{
alien_direction = 1.5;
down();
}
alienArray[i].x = alienArray[i].x + alien_direction;//move to the right
//alienArray[i].y = -alienArray[i].y
}
}
function down():void {
for (var i:int = 0; i< alienArray.length; i++)
{
alienArray[i].y += 5 //VspaceBetweenInvaders + 150;
}
}
It worked! Thanks I do appreciate
But there is an issue with the last column of my aliens. When the aliens hit the right hand side of the screen, they move towards the alien column before them (i.e. the 9th column) and over time their position becomes the same as the aliens in the 9th column
How can I make them to retain the column spacing while they move horizontally and vertically downwards?
The last code works fine to me, buy maybe this solve your problem
var alien_direction:Number = 1.5;
var alienArray:Array = new Array();
var columns = 10;
var rows = 5;
var HspaceBetweenInvaders = 60;
var VspaceBetweenInvaders = 40;
for (var i:int=0; i<columns; i++)
{
for (var j:int=0; j<rows; j++)
{
var alien_mc:mcAlien = new mcAlien();
alien_mc.x = i * HspaceBetweenInvaders + 55;
alien_mc.y = j * VspaceBetweenInvaders + 150;
addChild(alien_mc);
alienArray.push(alien_mc);
}
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
for (var i:int = 0; i< alienArray.length; i++){
if (alienArray[i].x+alienArray[i].width > stage.stageWidth){
alien_direction = -1.5;
down();
}
else if (alienArray[i].x < 0 ){
alien_direction = 1.5;
down();
}
alienArray[i].x = alienArray[i].x + alien_direction;
}
}
function down():void{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
for (var i:int = 0; i< alienArray.length; i++){
alienArray[i].y += 5;//VspaceBetweenInvaders + 150;
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onEnterFrame(event:Event):void{
for (var i:int = 0; i< alienArray.length; i++){
(Math.random()*100<10)?shoot(i):null // each frame 10% probability of shot
// more code
}
function shoot(i):void{
var shoot:Shoot=new Shoot(); // Object from the library
shoot.x=alienArray[i].x
shoot.y=alienArray[i].y
}
I have the code below but it still wouldnt work when i test it
// make aliens drop bombs randomly
for (var bombNumber:int = 0; bombNumber< alienArray.length; bombNumber++)
{
var random:Number = Math.random();
if(Math.random()*100<10)
{
shoot(); // you need create this function
}
}
function shoot():void
{
// create bombs and put them in array
var bomb_mc:mcBomb = new mcBomb();
var bombArray:Array = new Array();
bomb_mc.x = alienArray[i].x;
bomb_mc.y = alienArray[i].y
bombArray.push(bomb_mc);
for (var i:int = 0; i< alienArray.length; i++)
{
bombArray[i].y += 10;
}
}
// make aliens drop bombs randomly
for (var bombNumber:int = 0; bombNumber< alienArray.length; bombNumber++)
{
var random:Number = Math.random();
if(Math.random()*100<10)
{
shoot(); // you need create this function
}
}
var bombArray:Array = new Array();
function shoot():void
{
// create bombs and put them in array
var bomb_mc:mcBomb = new mcBomb();
addChild(bomb_mc)
bomb_mc.x = alienArray[i].x;
bomb_mc.y = alienArray[i].y
bombArray.push(bomb_mc);
}
addEventListener(Event.ENTER_FRAME,moveBombs)
function moveBombs(e:Event):void{
for (var i:int = 0; i< alienArray.length; i++){
bombArray[i].y += 10;
}
}
the code above seemed not to work so i modified it as follows
function onEnterFrame (event:Event): void
{
// make aliens drop bombs randomly
for (var bombNumber:int = 0; bombNumber< alienArray.length; bombNumber++)
{
var random:Number = Math.random();
if(Math.random()*100<10)
{
shoot();
}
}
function shoot():void
{
var random:Number = Math.random();
// create bombs and put them in array
var bomb_mc:mcBomb = new mcBomb();
addChild(bomb_mc);
bomb_mc.x = Math.random()*1280;
bomb_mc.y = Math.random()*800;
bombArray.push(bomb_mc);
for (var i:int = 0; i< alienArray.length; i++)
{
bombArray[i].y += 10;
}
}
}
However, I still have issues with the aliens droping the bombs.
at the moment bombs are randomly displayed on the screen. I tried replacing this section
bomb_mc.x = Math.random()*1280;
bomb_mc.y = Math.random()*800;
with
bomb_mc.x = Math.random()*alienArray[i].x;
bomb_mc.y = Math.random()*alienArray[i].y;
but it still wouldnt work.
i really want the aliens to drop about 5 bombs randomly and then delay for few seconds before droping more.
North America
Europe, Middle East and Africa
Asia Pacific