7 Replies Latest reply: Apr 12, 2012 12:36 PM by Ned Murphy RSS

    How do I add multiple items to a hitTestObject?

    le_mac_man Community Member

      I have a horizontally scrolling flying game involving a character and diferent types of food moving across the screen from right to left which the character has to collect ("hit"). I have the hitTestObject working for 2 different food movieclips with one item belonging to a healthyList, and the other to a junkList.

       

      The problem is I don't know how to add more items (movieclips) to each of the lists, so that I have for example 6 items in the healthyList and 4 items in the junkList.

       

       

       

      Below is the code in my Engine.as file. The red highlighted text is the code that creates the problem. I thought that I just needed to add this line in order to add another item to the "healthyList" of food types, but I obviously have it in the wrong place or have the wrong code completely.

       

      package

      {

          import flash.display.MovieClip;

          import flash.display.Stage;

          import flash.events.Event;

       

          public class Engine extends MovieClip

          {

       

              private var numClouds:int = 5;

              private var healthyList:Array = new Array();

              private var junkList:Array = new Array();

              private var myFlying:Flying;

             

              public function Engine()

              {                   

                  myFlying = new Flying(stage);

                  stage.addChild(myFlying);

                 

                  myFlying.x = stage.stageWidth / 8;

                  myFlying.y = stage.stageHeight / 2;

                 

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

                  {

                      stage.addChildAt(new Cloud(stage), stage.getChildIndex(myFlying));

                  }

                 

                  addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

       

              }

              private function loop(e:Event) : void

              {

                  if (Math.floor(Math.random() * 90) == 5)

                  {

                      var healthy:Carrot = new Carrot(stage, myFlying);

                      var healthy:Broccoli = new Broccoli(stage, myFlying);

                     

                      healthy.addEventListener(Event.REMOVED_FROM_STAGE, removeHealthy, false, 0, true);

                     

                      healthyList.push(healthy);

                     

                      stage.addChild(healthy);

                     

                     

                 

                      var junk:Hotdog = new Hotdog(stage, myFlying);

                     

                      junk.addEventListener(Event.REMOVED_FROM_STAGE, removeJunk, false, 0, true);

                    

                      junkList.push(junk);

                     

                      stage.addChild(junk);

                  }

                 

                 

                 

              }

             

              private function removeHealthy(e:Event)

              {

                  healthyList.splice(healthyList.indexOf(e.currentTarget), 1);

              }

             

             

            

              private function removeJunk(e:Event)

              {

                  junkList.splice(junkList.indexOf(e.currentTarget), 1);

              }

             

       

          }

       

      }

       

       

       

       

       

      All help and advice very much appreciated.

      Thank you!

        • 1. Re: How do I add multiple items to a hitTestObject?
          Ned Murphy CommunityMVP

          The red line and the one before it create the problem.  You cannot be declaring the same variable twice in the same section of code.  You will either need to have different functions that you call to load different object types, or you will need to specify a different type within that function.

          • 2. Re: How do I add multiple items to a hitTestObject?
            le_mac_man Community Member

            Hi Ned,

             

            I added the red text below and the broccoli appeared along with the carrot and the hotdog. Even though it worked, is this the right thing to do each time I want to add another object? or is this code going to cause me problems later, or slow things down when it's running?

             

            Eg. for another item, do I add var healthy2.Pepper = new Pepper(stage, myFlying);

             

             

             

            private function loop(e:Event) : void

                    {

                        if (Math.floor(Math.random() * 120) ==50)

                        {

                            var healthy:Carrot = new Carrot(stage, myFlying);

                            healthy.addEventListener(Event.REMOVED_FROM_STAGE, removeHealthy, false, 0, true);

                            healthyList.push(healthy);

                            stage.addChild(healthy);

             

             

                            var healthy1:Broccoli = new Broccoli(stage, myFlying);

                            healthy1.addEventListener(Event.REMOVED_FROM_STAGE, removeHealthy, false, 0, true);

                            healthyList.push(healthy1);

                            stage.addChild(healthy1);

             

             

                            var junk:Hotdog = new Hotdog(stage, myFlying);

                            junk.addEventListener(Event.REMOVED_FROM_STAGE, removeJunk, false, 0, true);

                            junkList.push(junk);

                            stage.addChild(junk);

                        }

            • 3. Re: How do I add multiple items to a hitTestObject?
              Ned Murphy CommunityMVP

              That will add all of the items at the same time.  If that is what you want, then that is what you can use.  I would expect you want to add different objects at different times, which wold require a different approach.

              • 4. Re: How do I add multiple items to a hitTestObject?
                le_mac_man Community Member

                Ok, so guess what I'm going to ask you now :-)

                 

                How would I add different objects at different times? Is there a time delay or random feature which is used?

                 

                 

                 

                One other question, if you don't mind.

                 

                In the code, I have:

                 

                if (Math.floor(Math.random() * 120) ==50)

                 

                I'm a bit confused as to what exactly the numbers mean, and how they alter the frequency of the items appearing on stage. Is there a quick explanation?

                 

                 

                Many many thanks Mr Murphy.

                • 5. Re: How do I add multiple items to a hitTestObject?
                  Ned Murphy CommunityMVP

                  Yes, there could be a delay (up to you), and the selection of what to load and the delay could both be random (up to you).  You could have separate functions for loading each of the different objects and devise a way to call one of them randomly, or you could have one function that includes a random selection of which class of object to load and have it load the selected one.

                   

                  The line of code you show is acting like a time delay, though not a very efficient one.  Each time that function executes (it executes at the frame rate of your file), it is picking a random integer between 0 and 119 inclusive, and if that number picked is 50, the condition passes and the code in the conditional executes. 

                   

                  You should look up all of the methods involved to gain an understanding of what they do... especially the Math.random() method since that is probably going to be used in various places in your design.

                  • 6. Re: How do I add multiple items to a hitTestObject?
                    le_mac_man Community Member

                    Ned Murphy wrote:

                     

                    You could have separate functions for loading each of the different objects and devise a way to call one of them randomly, or you could have one function that includes a random selection of which class of object to load and have it load the selected one.

                     

                     

                    Obviously I don't expect you to just do the work for me, but how would I do what you said above? What methods or coding techniques would I need to know in order to carry this out because I'm a bit lost here with all this.

                     

                     

                     

                    Ned Murphy wrote:

                     

                    The line of code you show is acting like a time delay, though not a very efficient one.  Each time that function executes (it executes at the frame rate of your file), it is picking a random integer between 0 and 119 inclusive, and if that number picked is 50, the condition passes and the code in the conditional executes.

                     

                     

                    Why is it not effective? Is there any reason for me using this code at all? What purpose does it serve exactly? Does this determine how many of each of my objects are on the stage at any one time?

                    • 7. Re: How do I add multiple items to a hitTestObject?
                      Ned Murphy CommunityMVP

                      It's good that you don't expect me to do the work for you - saves me from letting you know that.  Doing the work is how you learn.  I am also not here to teach you, and it looks like you might need to get some basics under your belt before you can pursue something like what you are doing now.

                       

                      As I already said, the Math.random() method will be one of the key techniques you will need to use for anything you want to have happen randomly.

                       

                      Usually it comes in handy with randomly choosing an array's index, where the array stores the different items you want to randomly choose.  In your case, the array could store the names of the different functions you could call for each food item you want to add. Or you could have the classes of the objects in the array and randomly choose which one to load in...

                       

                         var foodClasses:Array = new Array("Carrots", "Broccoli", "Hotdog", etc...);

                       

                      this approach requires knowing how to dynamically create a new class instance using only a String value.... for example:

                       

                          var ClassRef:Class = Class(getDefinitionByName("className"));

                          var classInstance:* = new ClassRef();

                          addChild(classInstance);

                       

                      In that bit of code, the className would be whatever class name you randomly pulled from the array of the class names...

                       

                       

                      I didn't say it isn't effective, though it can easily fail being effective as well when your file starts getting bogged down with everything else that is going on.  I said it isn't efficient.  It is constantly fliipping a 120-sided coin, as fast as the file's frame rate to see if it comes up 50 or not.  You could use a setTimeout function or a Timer and get much more efficent performance instead... only processing the code when the time passes. 

                       

                      If you cannot understand how that one line of code works, you are probably in over your head and you need to back up to more basic learning before you can tackle something like what you seem to want to create here.