Hey everyone so I have a Movie Clip inside my platforms
movie clip called mcTaco
so platforms.mcTaco
. I add the platforms
and the player
which I check for hitTest collisions to the stage dynamically using code. When I check for the player
and platforms.mcTaco
collison everything works fine. It's when I duplicate the mcTaco
creating multiple instances of them inside the platforms
movieclip that I am having the main issues.
I know I have to add them to an array and use a for loop to check for the hitTest of the multiple platforms.mcTaco
instances but I don't know how to accomplish this. I am using an IDE Flash Develop for my code and here is what I have so far:
I add my Platforms to the stage like so:
//ADD PLATFORMS AND ENVIRONMENT
private function addPlatForms():void
{
//Add Obstacle Platforms
platforms = new mcPlatforms();
platforms.x = (stage.stageWidth / 2) - 80;
platforms.y = (stage.stageHeight / 2) + 165;
addChildAt(platforms, 1);
aPlatformArray.push(platforms);
//trace(aPlatformArray.length + " NPLATFORMS");
}
and my Player:
//Add Character
player = new mcPlayer();
player.x = (stage.stageWidth / 2) - 80;
player.y = (stage.stageHeight / 2) + 78;
addChildAt(player, 1);
and in my ENTER_FRAME Event Listener I am doing this to test the player
and platforms.mcTaco
hitTest like so:
private function tacoHitTestHandler():void
{
if (player.hitTestObject(platforms.mcTaco))
{
platforms.mcTaco.visible = false;
trace("HIT TACO");
}
}
I just need some help on figuring out how to go about doing this. I have my Taco Arrayprivate var aTacoArray:Array;
instantiated but I just don't know how to push the platforms.mcTaco
Movie clips into it to use the for loop to test it. Any help on how to go about doing this would be greatly appreciated. I just don't want to have to do the if (player.hitTestObject(platforms.mcTaco1) || player.hitTestObject(platforms.mcTaco2) etc...)
You need to loop children inside each of the platforms to get all tacos by their class or/and name.
private function tacoHitTestHandler():void
{
// Loop through the list of the platforms.
for (var i:int = 0; i < aPlatformArray.length; i++)
{
// Get a platform instance from the Array.
var aPlatform:mcPlatforms = aPlatformArray[i];
// Loop through the children of a platform.
for (var j:int = 0; j < aPlatform.numChildren; j++)
{
// Get a potential taco.
var aTaco:mcTaco = aPlatform.getChildAt(j) as mcTaco;
// Skip if it is not a taco.
if (!aTaco) continue;
// Ignore invisible objects.
if (!aTaco.visible) continue;
// Ignore wrong tacos by name.
if (aTaco.name != "mcTaco") continue;
// Hit test against taco.
if (player.hitTestObject(aTaco))
{
aTaco.visible = false;
trace("HIT TACO");
}
}
}
}
A few notes.
First, calling a class mcTaco and giving the same instance name mcTaco is asking for headache because there are moments where your code will not be sure, if you are referring a class of a declared instance.
Second, call your classes with uppercase head letter because it's nice. A nice scottish McTaco would suffice.