1573 posts
  • 7 Years of Membership
  • Affiliate Level 1
  • Author Level 2
  • Collector Level 2
+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.

1689 posts Envato Elite Author
  • Affiliate Level 2
  • Author Level 8
  • Collector Level 1
  • Top Monthly Author
+8 more
Tean
says

Push them in array as you create them.

1234 posts
  • 6 Years of Membership
  • Affiliate Level 3
  • Author Level 7
  • Collector Level 3
+7 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
  • 7 Years of Membership
  • Affiliate Level 3
  • Author Level 6
  • Collector Level 2
+5 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.

846 posts
  • Top Monthly Author
  • Weekly Top Seller
  • 7 Years of Membership
  • Author Level 7
+9 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
  • 7 Years of Membership
  • Affiliate Level 2
  • Author Level 2
  • Beta Tester
+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