0
votes

I'm working on a Flash racing game in AS3 where the player navigates through a course marked by cones. The cones and the car are contained within a movie clip 'gamesprite'. I created an array for the cones in order to check for collision between the car and each of the cones.

public var cones:Array;

public function findCones() 
    {
        cones = new Array();
        for(var i=0;i<gamesprite.numChildren;i++)
        {
            var mc = gamesprite.getChildAt(i);
            if (mc is Cone) 
            {
                cones.push(Cone);
                trace(cones);
            }
        }
    }

Later in my game loop function I have this to check for collisions between the gamesprite.car movie clip and each of the cones in the array.

        for(var j:Number=0;j<cones.length;j++)
            {
                if (gamesprite.car.hitTestObject (cones[j]))
                {
                    trace("cones and car colliding");
                }
            }

I receive this error when testing the game.

TypeError: Error #1034: Type Coercion failed: cannot convert Cone$ to flash.display.DisplayObject. at Racing/gameLoop()

I'm probably missing something simple but can't figure it out.

1

1 Answers

0
votes

You're pushing a wrong thing into your array. You have there cones.push(Cone); but instead you should cones.push(mc);