ATTN newbies: Things are not Names
... but Names are Things.
I've seen this issue crop up a lot lately, so I wanted to try to explain things in an easy to understand way.
It seems a lot of people new to programming, or migrating from as2 to as3 have a bit of confusion between variables and instancenames. An instancename is just a String attached to some instance. That's it. It will ALWAYS be in quotes. (Now, I bet someone will find an exception to prove me wrong).
You can think of an instancename as a nametag, and actual things as people at a party. People are obviously not the same thing as their nametags, but you can use their nametags to find the right person if you need to.
Variables are a more direct way of talking about a thing. Variables have a type which tells you what sort of thing you're talking about. Variables can also vary, meaning that they can change values. Imagine you're keeping track of the hottest girl at the party. hottestGirl could be a variable.
So, let's go through some scenarios.
You're at a party, and your friend points to a girl and says "Wow!".
Code:
var hottestGirl:Person = friend.girlWatched;
You go up to this girl and introduce yourself.
Code:
this.say(hottestGirl, "Hi, I'm "+this.name);
And you read her nametag
Code:
this.say(hottestGirl, "I see your name is "+hottestGirl.name+". I've always liked that name"); //turns out it's "Catherine"
At this point, some of you would like to do something like this:
Code:
this.askOut(Catherine);
You can't. Because "Catherine" is her NAME, not her self. The variable Catherine does NOT exist. But you can make one.
Code:
var Catherine:Person = hottestGirl;
Awesome. Now your friend points out another girl.
Code:
trace(hottestGirl == Catherine); //true
hottestGirl = friend.girlWatched;
trace(hottestGirl == Catherine); //false
Your friend goes to talk to the new girl
Code:
friend.say(hottestGirl, "I'm "+friend.name+". How YOU doin'?");
if (hottestGirl.name == "Gertrude"){
friend.say(hottestGirl, "I just remembered I have to go drink now.");
hottestGirl = Catherine;
}
I hope it makes a bit more sense now.
But how do you use a thing's name to talk to the thing?
Quote:
Originally Posted by 5TonsOfFlax
At this point, some of you would like to do something like this:
Code:
this.askOut(Catherine);
You can't. Because "Catherine" is her NAME, not her self. The variable Catherine does NOT exist. But you can make one.
Code:
var Catherine:Person = hottestGirl;
Thanks for posting this, it's helping me with a current problem I'm having, but I'm still stuck. Here's the code:
Code:
for (var i:int = 1; i < 5; i ++ )
{
var theText:String = "btn"+i+"_mc.txt_mc.txt_txt";
var theBtn:String = "btn"+i+"_mc.hit_mc";
this.getChildByName(theText).autoSize = true;
this.getChildByName(theBtn).width = this.getChildByName(theText).textWidth;
}
Doesn't work. Seems like I've got the girl's name tag, "theText," but I can't use it to give her a drink. I'm trying to use getChildByName but no sale.