|
-
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?
 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.
-
You should post the error message you get rather than just "it doesn't work". Makes it easier to debug.
I suspect that the error you are getting is about the autoSize line. Something about how that property may not exist.
When you use getChildByName, you get back a DisplayObject. DisplayObjects do not have an autoSize property. So, in order to treat your DisplayObject as a TextField, you must cast it.
Code:
var tf:TextField = getChildByName(theText) as TextField;
tf.autoSize = TextFieldAutoSize.LEFT;
this.getChildByName(theBtn).width = tf.textWidth;
Is there a reason you needed to use naming conventions and displaylist paths to access things rather than proper arrays and actual instances? Sometimes there is a legitimate reason, but usually things can be reworked to be cleaner on the code side.
-
 Originally Posted by 5TonsOfFlax
You should post the error message you get rather than just "it doesn't work". Makes it easier to debug
Sorry about that, here are the two errors:
1119: Access of possibly undefined property autoSize through a reference with static type flash.display:DisplayObject.
1119: Access of possibly undefined property textWidth through a reference with static type flash.display:DisplayObject.
So you were correct about that.
As for the array vs naming convention thing, I did it this way because it's the way I've always done this kind of thing. I guess an array would be smoother, but just using a for loop is a step for me. Still a newbie I guess. More of a designer, really, although I'm trying to get better at programming.
Anyway I'll give your method a go and let you know you it turns out. Thanks for your help --
Last edited by insomniaGFX; 04-28-2008 at 12:43 PM.
Reason: Message board is erasing half of my post for some reason
-
Didn't work
I tried the following 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";
var tf:TextField = getChildByName(theText) as TextField;
tf.autoSize = TextFieldAutoSize.LEFT;
this.getChildByName(theBtn).width = tf.textWidth;
}
And I got this error in the output window:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
It's the "as," I think, which evaluates rather than changes the type. This used to be so easy! This is exactly the kind of thing that makes my clients hate AS3. I tell them it's worth it...
-
I don't think that is the problem. You might be thinking of "is" rather than "as".
Regardless, this is getting away from the point of this thread. If you'd like to start another, I will gladly help you there.
-
I'll start a new thread. Thanks --
-
5TonsOfFlax -
You should also clarify that naming and retrieving your instances using the name property is a newbish AS2 habit that should probably be used sparingly if at all in AS3.
-
Indeed, that is my opinion too. I think the only reason that people do that is because they don't know the alternatives. And then some people just don't want to learn a better way. For the newbs and for the old farts who hate strong typing for some reason, names are still a viable choice. In the same sense that commuting by horse is still technically a viable choice.
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
|