Copy link to clipboard
Copied
Hello!
We have two main MovieClips, loaded from library.
var BigCircle:MovieClip = new BigCircle();
BigCircle.name = "BigCircle";
addChild(BigCircle);
var BigSquare:MovieClip = new BigSquare();
BigSquare.name = "BigSquare";
addChild(BigSquare);
When added, BigCircle has child with name SmallCircle, which itself has child SmallerCircle, which itself has child TinyCircle.
When added, BigSquare using ENTER_FRAME wants to get TinyCircle, for example its x.
BigSquare don't wanna see any vars, it wants a link!
In AS2 it was very easy:
trace(this._parent.BigCircle.SmallCircle.SmallerCircle.TinyCircle._x);
or
trace(_root.BigCircle.SmallCircle.SmallerCircle.TinyCircle._x);
AS3 has getChildByName. Let's try it:
trace(stage.getChildByName("BigCircle").getChildByName("SmallCircle").getChildByName("SmallerCircle").getChildByName("TinyCircle").x); //ERROR 1061, even if use only two get...
or
trace(stage["BigCircle"]["SmallCircle"]["SmallerCircle"]["TinyCircle"]); //ERROR, even if use only two [ ], or dots between them
or combine
trace(stage.getChildByName("BigCircle").["SmallCircle"]); //AND WHAT NEXT?
TASK:
Give BigSquare a link to TinyCircle.
PLEASE HELP!
The "name" property of an object is just a string value, not an instance name. You do not have a var named "bigCircle, you have a var namned bC.
You are treating the name property like it is an instance name when you use...
trace(MovieClip(root).bigCircle);
but it is not an instance name, it is just a string. You should instead be using either...
trace(MovieClip(root).bC);
or
trace(MovieClip(root).getObjectByName("bigCircle"));
Copy link to clipboard
Copied
When you say things have names, what names are you referring to? For what I show below for the inner circles, the names are manually added instance names.
You should not use the same variable name as the class of an object. Try to follow conventions and use words starting with uppercase letters for class names and words starting with lowercase letters for object names/vars. The same goes for the name property of the object... lowercase.
var bigCircle:MovieClip = new BigCircle();
bigCircle.name = "BigCircle";
addChild(bigCircle);
var bigSquare:MovieClip = new BigSquare();
bigSquare.name = "bigSquare";
addChild(bigSquare);
Then, whatever code you have inside bigSquare targeting targeting TinyCircle could use...
trace(MovieClip(parent).bigCircle.SmallCircle.SmallerCircle.TinyCircle.x);
Copy link to clipboard
Copied
I've just simplified my code to Circles and Square, but forgot about "You should not use the same variable name as the class of an object") Thank you
And what this code means :
MovieClip(parent)
?
When I type MovieClip( - the only Flash CS5.5 tip is: MovieClip()
Copy link to clipboard
Copied
If you want to do things similarly to the way you did in AS2, then when you use the "parent" or "root" references you need to specify the class of the object that the parent/root is. Most usually you can get away with using MovieClip(), although there are alternatives... you could even use Object(), but the bottom line is that AS3 requires you to cast the object to an appropriate classt... So where you might have used the following for AS2...
_parent.whatever...
_root.whatever...
in AS3 you need to use...
MovieClip(parent).whatever...
MovieClip(root).whatever...
Copy link to clipboard
Copied
Well, it seems clear, but still not working( Still the same example, plus
bigSquare.addEventListener(Event.ENTER_FRAME, function(e:Event):void
{
trace(MovieClip(root).bigCircle);
});
It seems bigSquare and bigCircle don't even know, where they are added. It is root, isn't it? Or the mistake is in function, I need e.target ?
Copy link to clipboard
Copied
You shouldn't write the function into the listener like that, just keep it separate.
bigSquare.addEventListener(Event.ENTER_FRAME, clickbS);
function clickbS(e:Event):void {
trace(MovieClip(root).bigCircle);
}
Your latest code does not show where they are added, so I don't know where to tell you they are either, and can't see that you named them properly to agree with your listener/handler code.
If you are assigning the listener in the main timeline, which appears to be the case since you are targeting bigSquare, then you should be able to target bigCircle directly without the need for the parent/root reference. I thought you had the code in the timeline inside bigSquare such that you needed to use a parent/root reference.
Copy link to clipboard
Copied
Let's see the whole code (simplified) - main Class of .fla:
package
{
import flash.*;
import flash.display.*;
public class Whatever extends MovieClip
{
public function Whatever ()
{
var bC:MovieClip = new BigCircle();
bC.name = "BigCircle";
addChild(bC);
var bS:MovieClip = new BigSquare();
bS.name = "bigSquare";
addChild(bS);
// I suppose they are added to root, because nothing is before addChild
bS.addEventListener(Event.ENTER_FRAME, bS_ef);
}
function bS_ef(e:Event):void
{
trace(MovieClip(root).bigCircle);
}
}
}
Error #1069 = no bigCircle property
Copy link to clipboard
Copied
I didn't see the error message until I got to the end, but I was able to see the error well before I got there. You have not created an object called bigCircle, so you cannot expect to find one anywhere. You created one named bC.
Again, if you don't need to reference the root to target bC because it is already directly in scope with your code....
function bS_ef(e:Event):void
{
trace(bC); // this is all you need since bC is in scope of this function
}
Copy link to clipboard
Copied
It was an example, imagine that bS is in another package, it cannot know var bC.
I'm sorry, I typed the example a bit wrong, BigCircle, bigCircle... The scope is not in that. The corrected example:
package
{
import flash.*;
import flash.display.*;
public class Whatever extends MovieClip
{
public function Whatever()
{
var bC:MovieClip = new BigCircle();
bC.name = "bigCircle";
addChild(bC);
addChild(new Wtf());
}
}
}
package
{
import flash.*;
import flash.display.*;
import flash.events.*;
public class Wtf extends MovieClip
{
public function Wtf()
{
var bS:MovieClip = new BigSquare();
bS.name = "bigSquare";
addChild(bS);
bS.addEventListener(Event.ENTER_FRAME, bS_ef);
}
function bS_ef(e:Event):void
{
trace(MovieClip(root).bigCircle);
}
}
}
// ERROR
I still don'get it.
We have var, no matter how it is named, yes? yes!
We make it Movieclip, with name "bigCircle", add it to root, yes? yes!
Where it is (it is on the stage, but not answering)?! Please explain like for the idiots, I do not understand all the nuances of the English language.
Copy link to clipboard
Copied
The "name" property of an object is just a string value, not an instance name. You do not have a var named "bigCircle, you have a var namned bC.
You are treating the name property like it is an instance name when you use...
trace(MovieClip(root).bigCircle);
but it is not an instance name, it is just a string. You should instead be using either...
trace(MovieClip(root).bC);
or
trace(MovieClip(root).getObjectByName("bigCircle"));
Copy link to clipboard
Copied
My God, what is the 'name" property than? When I say
.name = "ZZZ"
WHAT I say, if it is only the string?! It's readOnly? I suppose no...
And what if I will not write .name="smth" , then getChildByName won't work? If I, for example, don't know var's name, created that movieClip?
So, if I've understood correctly
var's name = MovieClip's name.
And I don't need to write
.name = "smth"
at all...
?
Copy link to clipboard
Copied
I doubt you understand correctly based on what you just wrote. Don't confuse a var with a name properrty, they are different animals.
Do you need the name property, no. Unless you have a whole bunch of those BigCircle objects being created, all you will need is the bC variable to target the BigCircle instance. If you use the same variable name (bC) to create several, then it can come in handy to use the name property so that you can name them differently and target them separately, but still, you don't really need the name prperty. You could just as well store the insdtances in an array and use the array to target them..
You probably should spend some time reading the help documentation to learn about the name property. I've tried to explain what it is, but if you don't get it, then you just need time to get used to it I guess.
Copy link to clipboard
Copied
Thank you for all your help, Ned Murphy!
I've been using AS2 for 4 years, and had never been so angry doing simple things like now in AS3.
I've read about name property, example there is simple.
Please tell me the line in bS_ef, where bS traces tC.x
package
{
import flash.*;
import flash.display.*;
public class Whatever extends MovieClip
{
public function Whatever()
{
var bC:MovieClip = new BigCircle();
bC.name = "bigCircle";
addChild(bC);
var sC:MovieClip = new BigCircle();
sC.x = sC.y = 100;
sC.name = "smallCircle";
bC.addChild(sC);
var tC:MovieClip = new BigCircle();
tC.x = tC.y = 100;
tC.name = "tinyCircle";
sC.addChild(tC);
addChild(new Wtf());
}
}
}
package
{
import flash.*;
import flash.display.*;
import flash.events.*;
public class Wtf extends MovieClip
{
public function Wtf()
{
var bS:MovieClip = new BigSquare();
bS.name = "bigSquare";
addChild(bS);
bS.addEventListener(Event.ENTER_FRAME, bS_ef);
}
function bS_ef(e:Event):void
{
trace(??? "tinyCircle".x);
}
}
}
Copy link to clipboard
Copied
If you havbe used AS2 for a long time then you should know that using a string the way you just tried to will not work...
trace(??? "tinyCircle".x); // how would that work even in AS2?
Again, you are trying to use the name property as if it is an instance name, which it is not.
I have no idea of the scope of your code since you are just displaying classes without showing how you implement them, but I would say you would need to change it to something like...
trace(getChildByName("tinyCircle").x);
Copy link to clipboard
Copied
Trace(???) was just a mark where to change code)
That's what I read from an article "100 tips for ActionScript 3":
"Because every MovieClip, added to timeline, is dynamic, you can add a property, leading to new created child (and not only).
var another_mc:MovieClip = new MovieClip();
another_mc.name = "child_mc";
my_mc.addChild(another_mc);
// create link to child
my_mc.another_mc = another_mc;
// checking
trace(my_mc.another_mc); // [object MovieClip] "
This property (with the name = instance name) was created automatically in AS2, so every MovieClip 'knew' it's children.
Copy link to clipboard
Copied
So:
package
{
import flash.*;
import flash.display.*;
import flash.events.*;
public class Whatever extends MovieClip
{
public function Whatever()
{
var bC:MovieClip = new BigCircle();
bC.name = "bigCircle";
addChild(bC);
var sC:MovieClip = new BigCircle();
sC.x = sC.y = 100;
sC.name = "smallCircle";
bC.addChild(sC);
bC.sC = sC;
var tC:MovieClip = new BigCircle();
tC.x = tC.y = 100;
tC.name = "tinyCircle";
sC.addChild(tC);
sC.tC = tC;
addChild(new Wtf());
}
}
}
package
{
import flash.*;
import flash.display.*;
import flash.events.*;
public class Wtf extends MovieClip
{
public function Wtf()
{
var bS:MovieClip = new BigSquare();
bS.name = "bigSquare";
addChild(bS);
bS.addEventListener(Event.ENTER_FRAME, bS_ef);
}
function bS_ef(e:Event):void
{
var bC = MovieClip(root).getChildByName("bigCircle");
trace(bC.sC.tC.x); // WORKS!!!
}
}
}
Last question, I suppose, : How can I turn this:
var bC = MovieClip(root).getChildByName("bigCircle");
trace(bC.sC.tC.x);
to ONE line, may be not using new var?
trace(MovieClip(root).getChildByName("bigCircle").sC.tC.x); //Is not working, flash does not find property 'sC'
Copy link to clipboard
Copied
I've found the answer:
var bC = MovieClip(root).getChildByName("bigCircle");
trace(bC.sC.tC.x);
is similar to
trace(MovieClip(MovieClip(root).getChildByName("bigCircle")).sC.tC.x); // ActionScript 3
trace(_root.bC.sC.tC._x); // ActionScript 2
THANKS FOR HELP!
Copy link to clipboard
Copied
Anyone?