-
infidel!
movieclips on the stage?
Hey guys,
I have one question: is it possible with AS3 to see the list of the movieclips that are on stage?
Thanks a lot!
-
a.k.a gltovar
yes. what you do is click on the movie clip, then goto its properties (bottom of the screen) and give it a name. Then in you document class you can access that movieclip by the name you gave it.
-
infidel!
the list of movieclips I want to find out the names for are also added from my document class with addChild()
So no movieclips on the flash stage, just a blank keyframe and a document class.
Tnx!
-
a.k.a gltovar
ah whoops ^_^ man.
you can go about this a few ways.
one way is:
PHP Code:
var names:Array = new Array(); for(var i:int = 0; i < this.numChildren; i++) { name.push(this.getChildAt(i).name); }
so the 'this' can be replaces by any display object container, so its good for seeing a list of any child display objects
-
I wrote this yesterday to trace out the name of everything in the dom from any node downward:
PHP Code:
function deepTrace(d:DisplayObjectContainer, indent:String = ''):void{ var i:int = 0; var o:*; while(i < d.numChildren){ o = d.getChildAt(i); if(o is DisplayObjectContainer){ trace(indent + o.name); deepTrace(DisplayObjectContainer(o), indent + '\t'); } else { trace(indent + o.name); } i++; } }
// deepTrace(stage);
Depending on what you're trying to do though, it's probably easiest to just open an array and put the objects in there as you add them to the stage - that way you always have a handle on them and you don't have to do a search again later.
-
http://pat.theorigin.net
yay for self-referential functions. I salute you!
-
infidel!
so.. I tried this and i get that the names array contains "instance1" and "instance2" movieclips. My intention was to have a main mc called bg_mc and inside bg_mc I want to place the square_mc.
Code:
package
{
import flash.display.MovieClip;
/**
* ...
* @author infidel - aka Ovidiu Gheorghe
*/
public class main extends MovieClip
{
public var bg_mc:bg = new bg();
public var square_mc:square = new square();
public var names:Array = new Array();
public function main()
{
addBg();
addSquare();
//outputTrace();
getDisplayList();
}
private function getDisplayList():void
{
for(var i:int = 0; i < this.bg_mc.numChildren; i++)
{
names.push(this.bg_mc.getChildAt(i).name);
}
trace(names);
}
private function addSquare():void
{
bg_mc.addChild(square_mc);
}
private function addBg():void
{
addChild(bg_mc);
}
private function outputTrace():void
{
trace(bg_mc.x);
}
}
}
So.. by my logics trace(names); should return square_mc. What I am doing wrong ?
Tnx a bunch,
ovidiu
-
square_mc is a variable whose value is a square. That square was never given a name, so it has a default name of instance<something>.
If you want to see something other than the default when you trace the name property, you're going to have to set that name property to something other than the default.
Code:
public function main()
{
addBg();
addSquare();
square_mc.name = "bob";
//outputTrace();
getDisplayList();
}
-
infidel!
ok, got it! tnx a lot, really...
what about the "names" array? how come at trace(names) I see two members inside the array instead of only one?
tnx a lot!
-
It looks like bg_mc is probably linked to a library item. That item may include children for things such as Shapes to hold graphics, or other things defined inside it.
-
infidel!
ok.. I still got the error:
"TypeError: Error #1010: A term is undefined and has no properties.
at main/addMc()
at main()"
Code:
package
{
import flash.display.MovieClip;
/**
* ...
* @author
*/
public class main extends MovieClip
{
public var bg_mc:bg = new bg();
public var square_mc:square = new square();
public function main()
{
addMc();
}
private function addMc():void
{
this.addChild(bg_mc);
this.bg_mc.addChild(square_mc);
this.bg_mc.square_mc.name = "squareMc";
this.bg_mc.squareMc.alpha = 0.7;
}
}
}
what I am doin' wrong?..
tnx a bunch! Ovidiu
-
Adding a child does NOT make a property. That is,
Code:
this.addChild(bg_mc);
does NOT make this.bg_mc. Same with bg_mc.square_mc. Doesn't exist. But that's okay, since you don't need them. You've already got references to bg_mc and square_mc. Just use those.
Code:
private function addMc():void
{
this.addChild(bg_mc);
bg_mc.addChild(square_mc);
square_mc.name = "squareMc";
squareMc.alpha = 0.7;
}
-
infidel!
Code:
package
{
import flash.display.MovieClip;
/**
* ...
* @author
*/
public class main extends MovieClip
{
public var bg_mc:bg = new bg();
public var square_mc:square = new square();
public function main()
{
addMc();
}
private function addMc():void
{
this.addChild(bg_mc);
bg_mc.addChild(square_mc);
square_mc.name = "squareMc";
squareMc.alpha = 0.7;
}
}
}
this also returns:
1120: Access of undefined property squareMc.
tnx,
Ovidiu
-
Oops, yeah. That should be square_mc not squareMc.
Code:
private function addMc():void
{
this.addChild(bg_mc);
bg_mc.addChild(square_mc);
square_mc.name = "squareMc";
square_mc.alpha = 0.7;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|