Skip navigation
chiggy83
Currently Being Moderated

I need help

May 28, 2012 9:48 AM

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.

 
Replies
  • Currently Being Moderated
    May 28, 2012 10:18 AM   in reply to chiggy83

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2012 11:05 AM   in reply to chiggy83

    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;

        }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2012 11:22 AM   in reply to esdebon
    Indeed the condition would be better this way

     

     

     

    if (alienArray[i].x < 0 || alienArray[i].x > stage.stageWidth)

            {

                alien_direction * = -1;

                 down();

            }

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2012 3:18 PM   in reply to chiggy83

    could you give me the size of the stage you use, and the size of your alien in order to reproduce the problem

     
    |
    Mark as:
  • Currently Being Moderated
    May 28, 2012 3:42 PM   in reply to chiggy83

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

    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 29, 2012 8:24 AM   in reply to chiggy83

    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

     

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 5, 2012 7:57 AM   in reply to chiggy83

    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

     


     
    |
    Mark as:
  • Currently Being Moderated
    Jun 5, 2012 8:46 AM   in reply to chiggy83

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

           }

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points