Copy link to clipboard
Copied
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 p
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.x > stage.stageWidth ) {
alien_direction = -1.5;
}
else if (alienArray.x < 0 ) {
alien_direction = 1.5;
}
alienArray.x = alienArray.x + alien_direction; //move to the right
//alienArray.y = -alienArray.y
}
}
Copy link to clipboard
Copied
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.x > stage.stageWidth)
{
alien_direction = -1.5;
down();
}
else if (alienArray.x < 0 )
{
alien_direction = 1.5;
down();
}
alienArray.x = alienArray.x + alien_direction;//move to the right
//alienArray.y = -alienArray.y
}
}
function down():void {
for (var i:int = 0; i< alienArray.length; i++)
{
alienArray.y += 5 //VspaceBetweenInvaders + 150;
}
}
Copy link to clipboard
Copied
if (alienArray.x < 0 || alienArray.x > stage.stageWidth)
{
alien_direction * = -1;
down();
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
could you give me the size of the stage you use, and the size of your alien in order to reproduce the problem
Copy link to clipboard
Copied
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.x+alienArray.width > stage.stageWidth){
alien_direction = -1.5;
down();
}
else if (alienArray.x < 0 ){
alien_direction = 1.5;
down();
}
alienArray.x = alienArray.x + alien_direction;
}
}
function down():void{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
for (var i:int = 0; i< alienArray.length; i++){
alienArray.y += 5;//VspaceBetweenInvaders + 150;
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
Copy link to clipboard
Copied
this is a great solution.
thanks
Now that I have my aliens moving properly, I want to make them shoot bullets randomly
I thought of creatng an arraay for the bombs and populating about 10 bombs. But how can i make the aliens shoot the bullets randomly at given times?
Copy link to clipboard
Copied
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.x
shoot.y=alienArray.y
}
Copy link to clipboard
Copied
I'm sorry for replying late. Have been away
Back to the discussion, I don't understand the second line of the code:
(Math.random()*100<10)?shoot(i):null
could you explain a little bit more?
why do you have a question mark before shoot?
Copy link to clipboard
Copied
this line
(Math.random()*100<10)?shoot(i):null
is same to:
if(Math.random()*100<10){
shoot(); // you need create this function
}
where Math.random()*100 is a number between 0 and 100
then each frame have a 10% of probability of shoot
Copy link to clipboard
Copied
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.x;
bomb_mc.y = alienArray.y
bombArray.push(bomb_mc);
for (var i:int = 0; i< alienArray.length; i++)
{
bombArray.y += 10;
}
}
Copy link to clipboard
Copied
// 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.x;
bomb_mc.y = alienArray.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.y += 10;
}
}
Copy link to clipboard
Copied
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.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.x;
bomb_mc.y = Math.random()*alienArray.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.