1568 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
+2 more
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.

1677 posts Envato Elite Author
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
Tean says

Push them in array as you create them.

1234 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
+5 more
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
  • Has been part of the Envato Community for over 6 years
  • Has referred 50+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+4 more
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
  • Has referred 50+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+7 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
  • Has been part of the Envato Community for over 6 years
  • Has referred 10+ members
  • Has sold $100+ on Envato Market
  • Has been a beta tester for an Envato feature
+5 more
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