Friday, April 2, 2010

simple text field displaying variable...

Hi, in a simple AS2 .fla there was a text field labeled ''message_number'' which would display the variable ''message_number.''?Very straightforward.

How can a text field in AS3 be made to display this variable??

Thanks

simple text field displaying variable...

Assign and use the instance name of the textfield.?Example: msg_num is the inatance name.... msg_num.text = message_number;

simple text field displaying variable...

Thank you for your help.

I've selected the text field instance on the stage and using the Properties inspector changed it to ''Dynamic Text'' and labeled it: ''message_displayed''

but the following error message comes up when debugging:

WARNING: Text field variable names are not supported in ActionScript 3.0. The variable 'message_displayed' used for the text field 'message_displayed' will not be exported.

Am I assigning the name of this text field properly?

thanks again

Is there a variable that you created that shares the instance name you assigned to the textfield?

Thanks for you help.?Yes, the variable uses the same name as the textfield and is set at the beginning of the code with:

?message_displayed.text = 0;

I think in AS2 the dynamic textfield named message_displayed would display, in this case a number, without any further programming...

I just had a text field with the same name as the variable (a number) and changed this variable number by using ++ and - with functions called by buttons.

Hope that makes sense...it is a really simple .fla with this code (on another thread I am trying to (re)learn how to load swfs...so that part of the code may be wrong, too)...thanks for your help:

?import flash.display.MovieClip;

?import flash.ui.Mouse;

?import flash.events.MouseEvent;

?import flash.text.TextField;

?import flash.events.*;

?import flash.display.Loader;

?import flash.net.URLRequest;

message_displayed.text = 0;

message_requested.text = 0;

MovieClip(root).stage_target_mc.internal_stage_target_mc.load(''message_''+message _displayed+''.swf'');

next_btn.addEventListener(MouseEvent.MOUSE_DOWN, next_message);

function next_message(event:MouseEvent):void {

?message_displayed = ++message_displayed;

?MovieClip(root).stage_target_mc.internal_stage_target_mc.load(''message_''+messag e_displayed+''.swf'');

};

back_btn.addEventListener(MouseEvent.MOUSE_DOWN, back_message);

function back_message(event:MouseEvent):void {

?message_displayed = message_displayed - 1;

?MovieClip(root).stage_target_mc.internal_stage_target_mc.load(''message_''+messag e_displayed+''.swf'');

};

request_btn.addEventListener(MouseEvent.MOUSE_DOWN, request_message);

function request_message(event:MouseEvent):void {

?message_displayed = message_requested;

?MovieClip(root).stage_target_mc.internal_stage_target_mc.load(''message_''+messag e_displayed+''.swf'');

};

MovieClip(root).stop();

Yes, AS2 made it a little easier to use textfields as variables, but they could troublesome to work with as well.

In your code you don't have a variable sharing the name of the textfield instance, and you shouldn't.?That would cause a namespace conflict.?You could create a variable separate from the textfield that you use to manipulate the value of, but it cannot have the same name.

Textfields work with strings, not numeric values, so when you are reading or assigning values, you may need to convert to numbers or strings as appropriate.?I've marked up your code such that it should work okay using the textfield alone.

Also, assuming you aren't creating separate class files, you don't need to import any of the things you have lined up at the start.?Flash will automatically take care of acquiring those particular ones, though there are others that you do need to import.

message_displayed.text = ''0'';
message_requested.text = ''0'';
?
MovieClip(root).stage_target_mc.internal_stage_target_mc.load(new URLRequest(''message_''+message_displayed.text+''.swf''));
?
next_btn.addEventListener(MouseEvent.MOUSE_DOWN, next_message);
?
function next_message(event:MouseEvent):void {
?message_displayed.text = String(Number(message_displayed.text )+1);
?MovieClip(root).stage_target_mc.internal_stage_target_mc.load(new URLRequest(''message_''+message_displayed.text+''.swf''));
}
?
back_btn.addEventListener(MouseEvent.MOUSE_DOWN, back_message);
?
function back_message(event:MouseEvent):void {
?message_displayed.text = String(Number(message_displayed.text )-1);
?MovieClip(root).stage_target_mc.internal_stage_target_mc.load(new URLRequest(''message_''+message_displayed.text+''.swf''));
}
?
request_btn.addEventListener(MouseEvent.MOUSE_DOWN, request_message);
?
function request_message(event:MouseEvent):void {
?message_displayed.text = message_requested.text;
?MovieClip(root).stage_target_mc.internal_stage_target_mc.load(new URLRequest(''message_''+message_displayed.text+''.swf''));
}

MovieClip(root).stop();

Thank you thank you thank you!!!?Thanks Ned, you saved the day!?Wow...it will unfortunately take me the rest of the evening to implement this code change, but, THANK YOU for helping to improve (considerably) my understanding of AS3.?Thank you, thank you very much for your time...I wish there were a way to give 500 points.?

You're welcome.

No comments:

Post a Comment