VCriver
VCriver

Reputation: 1

How to getChildAt a movie clip inside a movieclip?

I have a movie clip linkage named "trainglePoint" inside a movieclip with a "bgdemo" instance and I was wondering how will I get the "trianglePoint" to work.

also "gags" is the character that will get the "trianglePoint"

this is my code below, which doesn't work.

thanks!

edit: forgot to add that the 'trianglePoint' is put onto stage multiple times (dont know if that helps)

var pickUpsArray:Array = new Array();

stage.addEventListener (Event.ENTER_FRAME, pickUpItems);

public function pickUpItems (e:Event)
    {
            for (var i=0; i<numChildren;i++)
    {
        if (getChildAt(i) is bgdemo.trianglePoint)
        {
            pickUpsArray.push(getChildAt(i));
        }
    }

        for (var j=0; j<pickUpsArray.length;j++)
        {
            if (gags.hitTestObject (pickUpsArray[j]))
            {
                removeChild(pickUpsArray[j]);
                trace ("hitTestObject: YES");
            }
        }
    }

Upvotes: 0

Views: 285

Answers (1)

Fabrice Bacquart
Fabrice Bacquart

Reputation: 129

You should instead use GetChildByName(name). GetChildAt implies that you know where in the child layers the child you want is located. So if your child is named "trianglePoint" and you want to access it, just use getChildByName("trianglePoint");

There are several problems with the rest of your code. First, you push into an array but never remove from it. Using removechild does not remove the object from the array which means you will always test it for collision with "gags".

The other issue is that you are adding your object to the array in each frame. What you should do instead is get your object from GetChildByName, push it into the array THEN loop and interact with it.

I would do it like this:

import flash.display.MovieClip

public class bgdemo extends MovieClip
{
var pickUpsArray:Array

public function bgdemo()//constructor for your parent movieclip
{
   pickUpsArray = new Array();

  this.addEventListener (Event.ADDED_TO_STAGE, init_ok);
  //this is to ensure the parent movieclip (bgdemo) is on the stage and we can access its children.
}

private function init_ok(e:Event):void
{
  this.removeEventListener(Event.ADDED_TO_STAGE, init_ok);
  //removing the listener so that we only do this once
  pickUpsArray.push(this.getChildByName("trianglePoint"));
  stage.addEventListener (Event.ENTER_FRAME, pickUpItems);
}

private function pickUpItems (e:Event):void
{
    for (var j=0; j<pickUpsArray.length;j++)
    {
        if (gags.hitTestObject (pickUpsArray[j]))
        {
            removeChild(pickUpsArray[j]);
            pickUpsArray.splice(j, 1); //removing the object from the array so we can't collide with it anymore
            trace ("hitTestObject: YES");
        }
    }
}

}

You create a new Array in your bgdemo movieclip's constructor and add a listener to the fact that the movieclip is added to the stage. When the movieclip is added to the stage, (remove the "added to stage listener"), get your child, add it to the array and add a listener to enter frame. On each frame you then test every object in your array (only 1 at this point) and "gags". If gags and the object collide, you then remove the object from both the display list and the array.

Try that and tell me if that works (the way you want it to work). If it's not what you wanted, then please be more precise on your original post ;)

Upvotes: 1

Related Questions