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.
- Sold between 50 000 and 100 000 dollars
- Author was Featured
- Author had a Free File of the Month
- Exclusive Author
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Europe
- Has been a member for 3-4 years
- Referred between 10 and 49 users
Push them in array as you create them.
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…
- Author was Featured
- Bought between 10 and 49 items
- Exclusive Author
- Has been a member for 5-6 years
- Item was Featured
- Netherlands
- Referred between 50 and 99 users
- Sold between 10 000 and 50 000 dollars
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.
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
- Author was Featured
- Bought between 10 and 49 items
- Exclusive Author
- Has been a member for 4-5 years
- Item was Featured
- Referred between 50 and 99 users
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.
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;
}
}
