PDA

View Full Version : Targetting in AS3



Noahdecoco
June 2nd, 2009, 12:29 AM
Hey,

I think this is a common question for AS3, but I have to ask again, cuz I haven't found a clear answer yet.

It's about targeting movie clips... In AS2, you just target a movie clip like so :

trace (FirstMc.ChildMc1.ChildMc2.ChildMc3._x) // Will send out the x position of ChildMc3

But is AS3, i see that there's a function called getChildByName('put child name here'). But what if it's such a long way to go to that child??? Does it have to be:

FirstMc.getChildByName("ChildMc1"). getChildByName("ChildMc2"). getChildByName("ChildMc3").x

That just seems to tedious and I'm sure Adobe thought of a clean way of targetting??(i hope)

Someone please shed some light? I'm sure many can learn from this...

Cheers!

snickelfritz
June 2nd, 2009, 01:53 AM
trace (FirstMc.ChildMc1.ChildMc2.ChildMc3.x) // Will send out the x position of ChildMc3

Noahdecoco
June 2nd, 2009, 03:00 AM
Hi!

Thanks for the reply, but I'm sorry, it doesn't work in my case...

Let me try to explain what's going on. This is the first scenario, the last trace doesn't work.



var largeContainer:MovieClip = new MovieClip();
addChild(largeContainer)

var mediumContainer:MovieClip = new MovieClip();
largeContainer.addChild(mediumContainer);

trace (largeContainer) //Displays [object MovieClip] correctly
trace (largeContainer.mediumContainer) //Displays undefined


Ok, so the last trace doesn't work cuz I can't target the movie. However, if I give it an instancename "mC" and then try with a getChildByName, it works...



var largeContainer:MovieClip = new MovieClip();
addChild(largeContainer)

var mediumContainer:MovieClip = new MovieClip();
mediumContainer.name = "mC"
largeContainer.addChild(mediumContainer);

trace (largeContainer) //Displays [object MovieClip] correctly
trace (largeContainer.getChildByName("mC")) //Displays [object MovieClip] correctly


So, thats good if the MovieClips are nested shallow, but if they are deep, what is the path to target it? For example, I added another MovieClip to the mediumContainer. The last trace doesn't work.



var largeContainer:MovieClip = new MovieClip();
addChild(largeContainer)

var mediumContainer:MovieClip = new MovieClip();
mediumContainer.name = "mC"
largeContainer.addChild(mediumContainer);

var smallContainer:MovieClip = new MovieClip();
smallContainer.name = "sC"
mediumContainer.addChild(smallContainer)

trace (largeContainer) //Displays [object MovieClip] correctly
trace (largeContainer.getChildByName("mC")) //Displays [object MovieClip] correctly
trace (largeContainer.getChildByName("mc").getChildByName("sC")) //Gives the error :

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


So you see, I can't target that movieClip! And I don't get why. Someone tell me?

Thanks!

Noahdecoco
June 2nd, 2009, 03:45 AM
Ok, here's a way around it. I can figure out how to reach the deeper nested movieClips, but I find (and I think you'll will agree), that this method is simply too long and tedious. In anycase, I'm posting it up here for your viewing. If someone has a cleaner, nicer, better, stronger, faster way... I'm all ears...



function setItUp() {
var largeContainer:MovieClip = new MovieClip();
largeContainer.name = "lC";
addChild(largeContainer);

var mediumContainer:MovieClip = new MovieClip();
mediumContainer.name = "mC";
largeContainer.addChild(mediumContainer);

var smallContainer:MovieClip = new MovieClip();
smallContainer.name = "sC";
mediumContainer.addChild(smallContainer);
}

function traceItOut() {
trace(getChildByName("lC")); //Traces [object MovieClip]
var lC = getChildByName("lC");
trace(lC.getChildByName("mC")); //Traces [object MovieClip]
var mC = lC.getChildByName("mC");
trace(mC.getChildByName("sC")); //Traces [object MovieClip]
}

setItUp(); //First Function

traceItOut(); //Second Function

Noahdecoco
June 3rd, 2009, 12:34 AM
*bump*

bonnieraymond
June 3rd, 2009, 01:18 AM
trace (largeContainer.getChildByName("mc").getChildByName("sC"))this should be:

trace (MovieClip(largeContainer.getChildByName("mc")).getChildByName("sC"))but why you use getChildByName while you already have a reference to that MovieClip?


trace (largeContainer)
trace (mediumContainer)
trace (smallContainer)




trace (largeContainer) //Displays [object MovieClip] correctly
trace (largeContainer.mediumContainer) // this is wrong

//just try:
trace (mediumContainer)

Noahdecoco
June 3rd, 2009, 02:48 AM
Thanks bonnieraymond,

Appreciate your help. I realize that one can refer to it simply using the variable name. But I face an issue when I target the movie from another function. So I guess there is no other way but to keep using :



trace (MovieClip(largeContainer.getChildByName("mc")).getChildByName("sC"))

snickelfritz
June 3rd, 2009, 03:22 AM
Declare the variables outside of the function, then define them within the function.
The variables should then be accessible to other functions.


var largeContainer:MovieClip;
var mediumContainer:MovieClip;
var smallContainer:MovieClip;

function setItUp()
{
largeContainer = new MovieClip();
largeContainer.name = "lC";
addChild(largeContainer);

mediumContainer = new MovieClip();
mediumContainer.name = "mC";
largeContainer.addChild(mediumContainer);

smallContainer = new MovieClip();
smallContainer.name = "sC";
mediumContainer.addChild(smallContainer);
}

function traceItOut()
{
trace(largeContainer.name, mediumContainer.name, smallContainer.name);
// traces lC mC sC
}

setItUp();//First Function
traceItOut();//Second Function

Noahdecoco
June 3rd, 2009, 04:00 AM
Thats a good idea... :D

But just to get this completely clear... in AS3, you can't refer to objects using the dot syntax when creating these objects through code? It works if I manually draw symbols and place em inside each other. But not if I create the symbols through code...

I'm placing a file here for you to see... I think this is a bug with AS3...??

Noahdecoco
June 10th, 2009, 04:44 AM
Hey,

Don't mean to be a bother, but this thing is bothering me so much!!! Can someone please explain to me why this happens? And if there is a way around it besides calling the variable name directly...

Cheers!

tiagopoeta
June 17th, 2009, 11:16 AM
I found a way... this is not the perfect answer but it is the better I got til now:

var father_mc:MovieClip = new MovieClip();
var child1_mc:MovieClip = new MovieClip();

father_mc.addChild(child1_mc);
father_mc.child1_mc = child1_mc; // here I defined a property for the father movieclip with the name I want to call the child mc.
trace(father_mc.child1_mc); // now you can call it from any scope without using the getchildbyname() method


Please let me know if this works for you, or if some one has any better solution!

Thanks!:bu:

Noahdecoco
June 18th, 2009, 07:29 AM
Hey! Thanks for your interest. Yes, that does seem to be a friendlier way to do it. But I find it to be so inconvenient. Hope there some easier way to do all this in the future. If I find something, will let you know!

Cheers!