Skip navigation
Currently Being Moderated

How do I add multiple items to a hitTestObject?

Apr 11, 2012 2:36 PM

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 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.




    import flash.display.MovieClip;

    import flash.display.Stage;



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



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








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


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











        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!

  • Currently Being Moderated
    Apr 11, 2012 6:09 PM   in reply to le_mac_man

    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.

    Mark as:
  • Currently Being Moderated
    Apr 12, 2012 5:58 AM   in reply to le_mac_man

    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.

    Mark as:
  • Currently Being Moderated
    Apr 12, 2012 9:33 AM   in reply to le_mac_man

    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.

    Mark as:
  • Currently Being Moderated
    Apr 12, 2012 12:36 PM   in reply to le_mac_man

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



    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. 

    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