14 Replies Latest reply: Jun 5, 2012 10:41 AM by chiggy83 RSS

    I need help

    chiggy83 Community Member

      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.

        • 1. Re: I need help
          esdebon Community Member

          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

          • 2. Re: I need help
            chiggy83 Community Member

            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

             

                   

                                                    }

             

                                }

            • 3. Re: I need help
              esdebon Community Member

              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;

                  }

              }

              • 4. Re: I need help
                esdebon Community Member
                Indeed the condition would be better this way

                 

                 

                 

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

                        {

                            alien_direction * = -1;

                             down();

                        }

                • 5. Re: I need help
                  chiggy83 Community Member

                  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?

                  • 6. Re: I need help
                    esdebon Community Member

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

                    • 7. Re: I need help
                      esdebon Community Member

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

                      }

                      • 8. Re: I need help
                        chiggy83 Community Member

                        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?

                        • 9. Re: I need help
                          esdebon Community Member

                          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

                           

                          }

                          • 10. Re: I need help
                            chiggy83 Community Member

                            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?

                            • 11. Re: I need help
                              esdebon Community Member

                              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

                               


                              • 12. Re: I need help
                                chiggy83 Community Member

                                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;

                                                                             }

                                 

                                 

                                }

                                • 13. Re: I need help
                                  esdebon Community Member

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

                                         }

                                  }

                                  • 14. Re: I need help
                                    chiggy83 Community Member

                                    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.