00:00
00:00
Newgrounds Background Image Theme

cosoden just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3 Displaying Variables in textbox

30,039 Views | 5 Replies
New Topic Respond to this Topic

AS3: Main

What I'm going to explain?

We have a variable and we want to display it in a text box, for example a health variable. I also explain how to make a variable

How will we do it?

1) First draw a simple text box in your Flash with the text tool.

2) Select your text box and at properties around the upper left there is a dropdown menu.

3) Open the dropdown menu and select Dynamic Text.

4) Under the dropdown menu there is a little input Textbox, type the name of how you want it to be called, I chose healthShow

5) Now we'll start with the code, first add:

addEventListener(Event.ENTER_FRAME, healthScore);

Note that healthScore can be anything, it's just how you want to call the listener.

6) You need to now how to make a variable if not, read step 7 first
Afterwards we tell Flash what the function is of healthScore

function healthScore (event:Event){
healthShow.text = String ("Health:"+health);
}

Note that when we tell the name of the textbox we made, with us it's healthShow, you need to add .text behind it. I tell what healtShow is, a string, and tell what it is, the text between (). I say that the string consists of the text Health:, we need to put this in brackets, because we only want the text. We don't want to have in this text only Health:, we also want to show the value of our health variable, so we say to Flash: 'After the text Health: show the value of our health variable, we do this by putting after "Health:" this: +health

7) type in Flash in the AS box the following:

var health:Number = 3;

Note that you can also replace Number with int or uint. These are the differences
Number can display all types of numbers, whole or a real (eg. 6,459). Positive and negative

int can display only whole numbers and can't go lower than -3

uint can display only whole numbers and can't be negative

8) to help you guys out and to not get you confused I'll post here the complete code

// set up the health variable
var health:number = 3;


// add listener
addEventListener (Event.ENTER_FRAME, healthShow);
function healthShow (event:Event){healthScore.text = String ("Health:"+health);
}

It is possible that I've made a mistake, if so leave a comment or PM me and I'll try to help you out


Ruining your life since 2006

BBS Signature

Response to AS3 Displaying Variables in textbox 2007-10-11 12:12:56


At 10/11/07 11:35 AM, LilFugitive wrote: int can display only whole numbers and can't go lower than -3

what?
int stands for integer and can be values from about -64000 to about 64000 or something like that.
uint simply stands for unsigned integer and goes from 0 to 128000.

Take those numbers as a hint, they probably aren't correct...

Otherwise very good tutorial :D.

Response to AS3 Displaying Variables in textbox 2007-10-11 14:38:06


At 10/11/07 12:57 PM, LCurtis wrote: This tutorial is pretty useless.
Basically you could have re-written this whole tutorial as:
1. Declare a variable
-- Ex. var Health:Numer=1;
2. Draw a textbox on the stage and give it an instance name
-- Ex. HealthScore
3. Set the text property of HealthScore to your variable like so:
-- Ex. HealthScore.text = "Health" + Health;

This works the same way it does in AS2. And you dont need to cast your variable to a string.

You could have instead written a complete tutorial about adding a textbox to the stage using AS and modifying its formatting and maybe even show htmlText instead of just Text.

Maybe your right, but this is also to really explain why it is written so, people can just as well steal code, but I want them to understand it. Also you need to add a ENTER_FRAME listener, or else it will just have the value of the variable, but it won't change to the current value if the var changes


Ruining your life since 2006

BBS Signature

Response to AS3 Displaying Variables in textbox 2007-10-31 16:31:06


At 10/11/07 12:12 PM, Fickludd wrote: Take those numbers as a hint, they probably aren't correct...

not even close.

int goes from - (2^31 - 1) to 2^31, which is -2 147 483 647 to 2 147 483 648
uint uses ones compliment instead of twos compliment, so its 0 thru 2^32, which is 4 294 967 296


BBS Signature

Response to AS3 Displaying Variables in textbox 2009-10-25 22:08:11


Thanks for the explanations , helped a lot!

Response to AS3 Displaying Variables in textbox 2010-02-21 10:04:06


Thanks, this really helped!
I was looking for a tutorial like this!
+50 internetz for you!