1567 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 6-7 years
  • India
  • Referred between 1 and 9 users
  • Sold between 100 and 1 000 dollars
rahulbist says

hi everyone,

I have created movieclip instances via loop.

function loadXml():void { for(var i:Number = 0;i<8;i++) { this.addChild(mov);

var mov:myMovie = new myMovie();
mov.name = "mov" + i;

}

}

How to access these movieclips in another function.

function another():void { for(var i:Number = 0;i<8;i++) { this.getChilldByName(“mov” + i);

}

}

getChildByName is throwing error below :-

1061: Call to a possibly undefined method getChildByName through a reference with static type flash.display : DisplayObject.

can anyone tell me how to solve this problem?

thanks in advance.

1675 posts Envato Elite Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
+1 more
Tean says

Push them in array as you create them.

1234 posts
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 50 and 99 users
  • Sold between 50 000 and 100 000 dollars
  • United States
MBMedia says

Tean’s solution is a good idea in most cases, it’s good to get used to storing lists of things in arrays for easier access later.

But as for your actual error, it seems that wherever that function is, when you reference “this” it seems to be typed as a “DisplayObject”. DisplayObject doesn’t contain the method getChildByName, DisplayObjectContainer does. So if your reference to “this” is truly the object containing the movieclips, try casting it to a DisplayObjectContainer as you call the method. (you may need to import flash.display.DisplayObjectContainer)

(this as DisplayObjectContainer).getChildByName("mov"+i);

But to be honest I’ve never seen a situation where you actually have “this” be the type DisplayObject rather than a more high level class like MovieClip or Sprite (which descend from DisplayObjectContainer, and would work properly)...so you may just plain have the function in a place where “this” isn’t the same object you think it is…

723 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 6-7 years
  • Netherlands
  • Referred between 50 and 99 users
  • Sold between 10 000 and 50 000 dollars
BigZ says
var i:uint

for(i = 0; i < 8; i++) 
{
    var mov:myMovie = new myMovie();
    mov.name = "mov"+i;
    this.addChild(mov)
}

for(i = 0; i < 8; i++) 
{
    trace(this.getChildByName("mov" + i))
}

This works for me, are you doing the same?

I dont know if it is the formatting in the forums, but it seems you are doing this.addChild(mov) before you have created mov.

843 posts
  • Elite Author
  • Exclusive Author
  • Has been a member for 5-6 years
  • Won a Competition
  • Sold between 100 000 and 250 000 dollars
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+2 more
marcfolio says

Not knowing the specifics of your set up, but you might be able to drop the “this”

If “this” is the stage I’d say drop it. Or create a parent MovieClip and add all the items to it (mc.addChild).

Then it’s easy to “mc.getChildByName”.

OR push them all to an array then you can use the TweenMax.allTo… Very handy.

2309 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 100 and 1 000 dollars
  • United States
theflyingtinman says

Simple implementation of the suggestions made above :


var aMovies:Array = new Array();

function loadXml():void
{
    for(var i:Number = 0;i<8;i++)
    {
        aMovies.push(addChild(new myMovie()))
    }
}

function another():void
{
    for(var i:Number = 0;i<8;i++)
    {
        //Do something with stored movieclips. eg:
        aMovies[i].x = i*aMovies[i].width;
    }
}

by
by
by
by
by
by