Skip to main content
April 11, 2012
Question

How do I add multiple items to a hitTestObject?

  • April 11, 2012
  • 1 reply
  • 2093 views

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!

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
April 12, 2012

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.

April 12, 2012

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

            }

Ned Murphy
Legend
April 12, 2012

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.