15 Replies Latest reply on Nov 8, 2011 9:23 AM by Peter Celuch

    Help Please - Objects to Display

    McbainGames11 Level 2

      Hey how can i get my objects to display? In my class i have a function that creates objects and they fall randomly in rows but only 1 is displaying. And i used trace(); to test how many on stage and it said 4 but only 1 starting falling.

       

      I think there is something wrong with the method i am using to create the objects in rows.

       

      Heres the code:

       

      package

      {

       

          import flash.display.MovieClip;

       

          import flash.events.Event;

       

          import flash.events.MouseEvent;

       

          import flash.events.TimerEvent;

       

          import flash.utils.Timer;

       

          public class Main extends MovieClip

          {

       

              public var enemyA:Array = [];

       

              public var birdA:Array = [];

       

              public var rowNum:int = 1;

       

              private var gap:int = 100;

       

              private var obj_no = 2;

       

              public var timer:int = 0;

       

              public var time_limit = 10;

       

              public var TimerSpeed:uint = 1000;

       

              public var MyTimer:Timer = new Timer(TimerSpeed);

       

              public var enemy1:mychar = new mychar();

       

              public var TheBird:BirdChar = new BirdChar();

       

              public function Main()

              {

       

                  // Constructor Code

       

              }

              public function activateGameControls()

              {

       

                  createEnemyF();

       

                  createBirdF();

       

                  this.addEventListener(Event.ENTER_FRAME,loopF);

       

                  MyTimer.addEventListener(TimerEvent.TIMER, TimerHandler);

       

                  MyTimer.start();

       

              }

              public function deactivateGameControls()

              {

       

                  addChild(TheBird);

       

                  addChild(enemy1);

       

                  removeChild(TheBird);

       

                  removeChild(enemy1);

       

              }

              public function createEnemyF()

              {

       

                  for (var i:int = 0; i < rowNum; i++)

                  {

                      for (var j:int = 0; j < obj_no; j++)

                      {

       

                          enemy1.x = Math.random() * stage.stageWidth - enemy1.width;

       

                          enemy1.y =  -  i * (gap + enemy1.height) - 30.65;

       

                          enemyA.push(enemy1);

       

                          addChild(enemy1);

       

                          trace(enemy1.x, enemy1.y);

       

                      }

                  }

              }

              public function createBirdF()

              {

       

                  TheBird.x = 270.95;

       

                  TheBird.y = 350.95;

       

                  birdA.push(TheBird);

       

                  addChild(TheBird);

       

              }

              public function loopF(event:Event)

              {

       

                  updateEnemyPositionsF();

       

                  updateBirdPositionsF();

       

                  hitTestF();

       

              }

              public function updateEnemyPositionsF()

              {

       

                  enemy1.y +=  2;

       

              }

              public function updateBirdPositionsF()

              {

       

                  TheBird.x = mouseX;

       

              }

              public function hitTestF()

              {

       

                  if (TheBird.hitTestObject(enemy1))

                  {

       

                      gotoAndPlay(5);

       

                      deactivateGameControls();

       

                      trace('The Bird Hit Enemy 1');

       

                  }

       

              }

              function TimerHandler(event:TimerEvent)

              {

       

                  timer +=  1;

       

                  if (timer == 50)

                  {

       

                      MyTimer.removeEventListener(TimerEvent.TIMER, TimerHandler);

       

                      MyTimer.stop();

       

                      gotoAndPlay(4);

       

                  }

                  else if (timer == time_limit)

                  {

       

                      gotoAndPlay(5);

       

                      deactivateGameControls();

       

                  }

       

              }

       

          }

       

      }

        • 1. Re: Help Please - Objects to Display
          Peter Celuch Level 4

          This should start moving them all:

           

          public function updateEnemyPositionsF()
          {
               for(var i:int = 0; i < enemyA.length; i++) {
                    var enemy:DisplayObject = enemyA[i] as DisplayObject;
                    enemy.y +=  2;
               }
          
          }
          

           

          The problem was that you moved only enemy1 object, which was the last enemy you have created. Luckily you have pushed all your enemies to the enemyA Array, so you can easily run through them and move each and every one of them.

          1 person found this helpful
          • 2. Re: Help Please - Objects to Display
            McbainGames11 Level 2

            Couldn't get them to go but i tried this code and going to work from this:

             

            //------------------------------------------//

             

            public function createEnemyF()

            {

             

            for (var i:int = 0; i < 10; i++)

            {

             

                enemy1.x = Math.random() * stage.stageWidth - enemy1.width;

             

                enemy1.y =  -  i * (gap + enemy1.height) - 30.65;

             

                enemyA.push(enemy1);

             

                addChild(enemy1);

             

                trace(enemy1.x, enemy1.y);

             

            }

             

            }

             

            //------------------------------------------//

             

            Output:

             

            -47.3 -30.65

             

            257.3 -207.65

             

            379.75 -384.65

             

            356.1 -561.65

             

            59 -738.65

             

            319.3 -915.65

             

            332.6 -1092.65

             

            -78.55 -1269.65

             

            109.15 -1446.65

             

            197 -1623.65

             

            //------------------------------------------//

             

            What do you suggest?

             

            I want objects to fall from sky in rows of 4 and all objects in the rows are placed in the rows randomly.

             

            How do i achieve this?

             

            Thanks for your help!

             

            //------------------------------------------//

            • 3. Re: Help Please - Objects to Display
              Peter Celuch Level 4

              Take a look at this example (snow animation), download the source and take a little time to study it. It's exactly what you want to do, except the flakes keep moving from side to side, but it's just an animation inside movieclip so you remove the timeline animation and you have EXACTLY what you want. This should answer all your questions.

               

              Snow sample: http://dev.flashlabs.eu/examples/snow/

              You can download the source here: http://dev.flashlabs.eu/examples/snow/source.zip

              • 4. Re: Help Please - Objects to Display
                McbainGames11 Level 2

                Same sort of thing but not exactly...

                 

                What i want is for objects to be added at position y=30.65 placed in the rows in a random order and fall down in ROWS.

                Bit harder job now with those things added. A bit of math involved which i don't know how to go about it.

                 

                So basically i want things placed in rows and a variable that determines the gap inbetween each row. This is code i have so far.

                 

                public function createEnemyF()

                {

                 

                for (var i:int = 0; i < 10; i++)

                {

                 

                    enemy1.x = Math.random() * stage.stageWidth - enemy1.width;

                 

                    enemy1.y =  -  i * (gap + enemy1.height) - 30.65;

                 

                    enemyA.push(enemy1);

                 

                    addChild(enemy1);

                 

                    trace(enemy1.x, enemy1.y);

                 

                }

                 

                }

                 

                As you seen the output is in previous post.

                 

                Thanks for help, really appreciate it!

                • 5. Re: Help Please - Objects to Display
                  Peter Celuch Level 4

                  The example I sent you should illustrate how to deploy any number of objects and update their properties in loop.

                  Your basic problem is, that you want to iterate through single item (instead of an array), enemy1.

                  enemy1 holds reference to only one object. I don't see any instantiation of enemies, too.

                   

                  You should have something like this:

                   

                  var enemyA:Array = [];
                  
                  const MAX_ENEMIES:int = 10;
                  
                  for(var i:int = 0; i < MAX_ENEMIES; i++) {
                       var enemy:DisplayObject = new Enemy();
                       enemy.x = Math.random() * stage.stageWidth - enemy.width;
                       enemy.y = - i * (gap + enemy.height) - 30.65;
                       addChild(enemy);
                       trace(enemy.x, enemy.y);
                       enemyA.push(enemy);
                  }
                  
                  then onEnterFrame or timer call updateEnemyPositions
                  
                  function updateEnemyPositions():void {
                       for(var i:int = 0; i < enemyA.length; i++) {
                            var enemy:DisplayObject = enemyA[i] as DisplayObject;
                            enemy.y += 2;
                       }
                  }
                  
                  • 6. Re: Help Please - Objects to Display
                    McbainGames11 Level 2

                    Sorry, but i don't get your method on how to do this Here is my code, please give me some steps on exactly what to do and comments on why you are doing it please ..

                     

                     

                    package

                    {

                     

                        import flash.display.MovieClip;

                     

                        import flash.events.Event;

                     

                        import flash.events.MouseEvent;

                     

                        import flash.events.TimerEvent;

                     

                        import flash.utils.Timer;

                     

                        import flash.display.DisplayObject;

                     

                        public class Main extends MovieClip

                        {

                     

                            public var enemyA:Array = [];

                     

                            public var birdA:Array = [];

                     

                            private var gap:int = 100;

                     

                            public var timer:int = 0;

                     

                            public var time_limit = 10;

                     

                            public var TimerSpeed:uint = 1000;

                     

                            public var MyTimer:Timer = new Timer(TimerSpeed);

                     

                            public var enemy1:mychar = new mychar;

                     

                            public var TheBird:BirdChar = new BirdChar;

                     

                            public function Main()

                            {

                     

                                // Constructor Code

                     

                            }

                            public function activateGameControls()

                            {

                     

                                createEnemyF();

                     

                                createBirdF();

                     

                                this.addEventListener(Event.ENTER_FRAME,loopF);

                     

                                MyTimer.addEventListener(TimerEvent.TIMER,TimerHandler);

                     

                                MyTimer.start();

                     

                            }

                            public function deactivateGameControls()

                            {

                     

                                addChild(TheBird);

                     

                                addChild(enemy1);

                     

                                removeChild(TheBird);

                     

                                removeChild(enemy1);

                     

                            }

                    //here is the function i need fixed! I want enemy1 added randomly into rows with the gap.

                            public function createEnemyF()

                            {

                     

                                for (var i:int = 0; i < 10; i++)

                                {

                     

                                    enemy1.x = Math.random() * stage.stageWidth - enemy1.width;

                     

                                    enemy1.y =  -  i * (gap + enemy1.height) - 30.65;

                     

                                    enemyA.push(enemy1);

                     

                                    addChild(enemy1);

                     

                                    trace(enemy1.x, enemy1.y);

                     

                                }

                     

                            }

                            public function createBirdF()

                            {

                     

                                TheBird.x = 270.95;

                     

                                TheBird.y = 350.95;

                     

                                birdA.push(TheBird);

                     

                                addChild(TheBird);

                     

                            }

                            public function loopF(event:Event)

                            {

                     

                                updateEnemyPositionsF();

                     

                                updateBirdPositionsF();

                     

                                hitTestF();

                     

                            }

                            public function updateEnemyPositionsF()

                            {

                     

                                enemy1.y +=  2;

                     

                            }

                            public function updateBirdPositionsF()

                            {

                     

                                TheBird.x = mouseX;

                     

                            }

                            public function hitTestF()

                            {

                     

                                if (TheBird.hitTestObject(enemy1))

                                {

                     

                                    gotoAndPlay(5);

                     

                                    deactivateGameControls();

                     

                                    trace('The Bird Hit Enemy 1');

                     

                                }

                     

                            }

                            function TimerHandler(event:TimerEvent)

                            {

                     

                                timer +=  1;

                     

                                if ((timer == 50))

                                {

                     

                                    MyTimer.removeEventListener(TimerEvent.TIMER,TimerHandler);

                     

                                    MyTimer.stop();

                     

                                    gotoAndPlay(4);

                     

                                }

                                else if ((timer == time_limit))

                                {

                     

                                    gotoAndPlay(5);

                     

                                    deactivateGameControls();

                     

                                }

                     

                            }

                     

                        }

                     

                    }

                     

                     

                    I really want to finish this so i can finish my game, you are a good help so far!

                    • 7. Re: Help Please - Objects to Display
                      Peter Celuch Level 4

                      1) what is mychar? Considering how you use it, it should be class asigned to a MovieClip symbol in your library

                           - the name indicates some sort of text, which is inappropriate

                           - class names should start with capital letter

                           - I would name it Enemy

                       

                      2) I noticed you create only one instance of your enemy (new mychar())

                           - if you want multiple enemies to fall from the sky, you need to create multiple enemies (in for loop..)

                       

                      3) if you want to move each and every enemy, you have to store them in the Array. You can do that when you create them (in the for loop)

                       

                      4) you move your enemies by getting then one by one from the array and incrementing their "y" value

                       

                      5) I would definitely get rid of "enemy1" variable - it's useless. Use enemyA instead - it can hold all your enemies

                       

                      Explanation of my code:

                       

                      // an array in which you will store references to your enemies
                      var enemyA:Array = [];
                       
                      // constant defining how many enemies you want there to be
                      const MAX_ENEMIES:int = 10;
                      
                      // loop for creation on your enemies
                      for(var i:int = 0; i < MAX_ENEMIES; i++) {
                           
                      
                           // local variable to temporarily store current enemy we're adding
                           var enemy:DisplayObject = new Enemy();
                           
                      
                           // set enemy's x position, random from left edge to the right edge of the screen
                           enemy.x = Math.random() * stage.stageWidth - enemy.width;
                           
                      
                           // set enemy's y position, in his own row. each enemy is a row higher than the previous one
                           enemy.y = - i * (gap + enemy.height) - 30.65;
                           
                      
                           // add the enemy to the display list so he is visible on the stage
                           addChild(enemy);
                           
                      
                           // trace his position
                           trace(enemy.x, enemy.y);
                           
                      
                           // store the enemy in the array so we can address him later
                           enemyA.push(enemy);
                      }
                       
                      //then onEnterFrame or timer call updateEnemyPositions
                      
                      
                      function updateEnemyPositions():void {
                           // loop for enemy movement - falling off the sky
                           for(var i:int = 0; i < enemyA.length; i++) {
                                
                      
                                // get a reference of i-th enemy in the array
                                var enemy:DisplayObject = enemyA[i] as DisplayObject;
                                
                      
                                // move enemy a bit more downwards
                                enemy.y += 2;
                           }
                      }
                      

                       

                      I really can't make it more simple, it can't be done any more simple. Please - read carefully 5 times and then try it.

                      I won't make the game for you, you have to understand what I'm saying and apply it to your game yourself. Otherwise you'll be never able to do it yourself, sorry.

                      • 8. Re: Help Please - Objects to Display
                        McbainGames11 Level 2

                        Very nice instructions!

                         

                        Works well but if it isn't to much i would like you to answer some questions with good detail so i can understand the 'fuzzy bits' of the code i havn't learnt yet.

                         

                        1. white did you make it a display objec in this line of code ? What does it help with? var enemy:DisplayObject = new Enemy();

                        (I have never used a display object).

                         

                        2. what is the class 'length' check for? I am guessing it checks for something in the array? Why have you used it?  for(var i:int = 0; i < enemyA.length; i++) {

                         

                        3. This line of code obsolutely boggles my mind as i have used every thing used in this haha. Like 'DisplayObject', enemyA[i]( what does putting the i in brackets do because i dont know how i is part of the array?), 'as'( what does as do?).

                        var enemy:DisplayObject = enemyA[i] as DisplayObject;

                         

                        4.And last but no least... How do i reference all my objects because the line that creates the objects (var enemy:DisplayObject = new Enemy();) is inside a function and i wan't to reference the enemies in hit test, remove child class's etc.

                         

                        The code i have learnt over past few weeks is making me a hell of a lot better programmer(mainly learning the abilities of class's).

                        Again.. I really would be stuffed without you and others who help me!

                         

                        Thanks.

                        • 9. Re: Help Please - Objects to Display
                          Peter Celuch Level 4

                          1. white did you make it a display objec in this line of code ? What does it help with? var enemy:DisplayObject = new Enemy();

                          (I have never used a display object).

                          I didn't know exactly what class did you use, but since it was on the stage, it has to be of type DisplayObject. I could have used as well MovieClip, since it's a symbol in library.. For what I wanted to do with it, DisplayObject was enough. See, MovieClip is also a DisplayObject, it inherits from it:

                          MovieClip  Inheritance Sprite Inheritance DisplayObjectContainer Inheritance InteractiveObject Inheritance DisplayObject Inheritance EventDispatcher Inheritance Object

                           

                           

                          2. what is the class 'length' check for? I am guessing it checks for something in the array? Why have you used it?  for(var i:int = 0; i < enemyA.length; i++) {

                          length is a property of class Array, it returns number of items in the array

                           

                           

                          3. This line of code obsolutely boggles my mind as i have used every thing used in this haha. Like 'DisplayObject', enemyA[i]( what does putting the i in brackets do because i dont know how i is part of the array?), 'as'( what does as do?).

                          var enemy:DisplayObject = enemyA[i] as DisplayObject;

                          enemy[i] means "i-th item in the array enemyA. "as DisplayObject" means that whatever is in that array at i-th position, I'm going to treat treah it as DisplayObject (because I know there's a DisplayObject there but the compiler doesn't know that)

                           

                           

                          4.And last but no least... How do i reference all my objects because the line that creates the objects (var enemy:DisplayObject = new Enemy();) is inside a function and i wan't to reference the enemies in hit test, remove child class's etc.

                          the var enemy:Enemy is only temporary variable, after execution of the function is over, the variable will no longer exist. I stored all the enemies in the array here: enemyA.push(enemy);

                          Take you time and familiarize yourself with the Array class: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html

                          1 person found this helpful
                          • 10. Re: Help Please - Objects to Display
                            McbainGames11 Level 2

                            Thanks!

                             

                            So what name do i call to reference all of my array objects for a hit test?

                            • 11. Re: Help Please - Objects to Display
                              Peter Celuch Level 4

                              The same principle as in updatepositions - loop through an array of enemies:

                               


                              for(var i:int = 0; i < enemyA.length; i++) {
                                   var enemy:DisplayObject = enemyA[i] as DisplayObject;
                                   trace("this is your enemy #" + i + ": " + enemy + ", do whatever with him.");
                              }
                              

                              You didn't look at the link I gave you, did you?

                              • 12. Re: Help Please - Objects to Display
                                McbainGames11 Level 2

                                I will look at the link but how do i make a hit test for ALL of the Enemies in the array? Whats the magic word haha?

                                • 13. Re: Help Please - Objects to Display
                                  Peter Celuch Level 4
                                  for(var i:int = 0; i < enemyA.length; i++) {
                                       var enemy:DisplayObject = enemyA[i] as DisplayObject;
                                       if(enemy.hitTestObject(myObjectThatIwantToCheckAgainst)) {
                                            trace("BAAAM! Enemy #" + i + " hit whatever he was supposed to hit.");
                                       }
                                  }
                                  


                                  • 14. Re: Help Please - Objects to Display
                                    McbainGames11 Level 2

                                    haha, why didn't i think of that, i think i over think things to much!

                                     

                                    Thanks, Problemo Solved