00:00
00:00
Newgrounds Background Image Theme

slugjuicedotcom 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!

AS:Variables New

2,669 Views | 10 Replies
New Topic Respond to this Topic

AS:Variables New 2007-01-28 15:04:58


AS:Main

now why would we need another AS:Variable? well.. I don't think the two other ones, yes mine to, contains all the info. but you can still check them out:
AS:Variables
AS:Variables more
I'm going to try to make people learn more from this the the old..

Declaring a variable
all tho you can just put variable_name=something;
the correct way of doing it is:
var name:type = info;
example:
var life:Number=3;
var day:String = "monday";
var hit:Boolan = false;

or you can use Arrays
AS:Arrays
AS:Arrays

What is a variable?
A variable is a way of storing information when your going to use it later.
it can be the life in a game, or the score. it can be the name of the player.
you can store a variable and then change it during the game. use your imagination!

Variable types
you have different types of variables.
Array
read AS:Array for more info on that
Number
This is used to store numbers. health, score, etc..
A number variable can be changed with math ( multiply, add, subtract, etc) Il go into that later.
String
A string is a variable containing text. It can be used for storing names, or storing info that you want to display as text.
Boolan
A boolan can store 2 things. true or false. (1 and 0) its used when you only need to check if the player can do something or not. like, lets say your making a RPG. and in the fighting you tell the player to attack. and then you have to wait for the enemy to attack the player before he can attack again. it only uses 1 bite of memory.. so its good when you want to save speed.
Other types
there are other types.. but this is the only one I use.

Changing a variable
there are several ways of changing a variable:
+=
this is the same as saying variable= variable+something
example:
var score:Number = 5;
score+=5;

score will now be 10.. because 5+5=10.
-=
this is the same as saying variable= variable-something
example:
var score:Number = 5;
score-=5;

score will now be 0.. because 5-5=0.
++
this is the same as saying variable+=1
--

this is the same as saying variable-=1
*=

this is the same as saying variable= variable*something
example:
var score:Number = 5;
score*=5;

score will now be 25.. because 5*5=25.
/=
this is the same as saying variable= variable+something
example:
var score:Number = 5;
score/=5;

score will now be 1.. because 5/5=1.
%=
now this one I have never used my self.. but this is what I was told it did:

thanks to authorblues
=)

It returns the remainder after division.

trace(5%2); // returns 1, since 5/2 = 2r1
trace(8%2); // returns 8, since 8/2 = 4r0
trace(8%3); // returns 2, since 8/3 = 2r2
trace(10%4); // returns 4, since 10/4 = 2r2

Reading a variable
there are two ways you can read a variable. show it to the player, or check it
first lets see how you show it to the player.
trace(variable_name);
this is only recommended when your trying to find an error in your code. it opens a new window with the content of the variable in it.

the other way is to have a text box display it.
just make a dynamic text box, open the properties panel and set the "var" to the name of the variable.

now on to the checking of a variable.
to check a variable you should use a if statement

first lets check a Number
var num:Number=5;
if(num==5){

will return true.. while
if(num==6){ will return false.

== is a way to see if its the same.. in English "num == 5" is the same as "num is 5".
!= is a way to see if its not the same.. in English "num != 5" is the same as "num is not 5".
=== will check if its the same info, and the same type 5===5 is true but "5"===5 is false

when checking a boolan you can just do
var hit:Boolan = true;
now
if(hit){
will return true.. but
if(!hit){
will return false

in English if(hit){ will be "if hit is true" and if(!hit){ will be "if hit is false.

Thats all for now
I hope I didn't forget to much
And I hope I was able to reach my goal and made a tutorial that contains more the the others thats already made.
and sorry for my English.. I'm Norwegian!

Response to AS:Variables New 2007-01-28 15:18:32


Hope you don't mind me extending this.

Extension on variable types:

MovieClip - A reference to a MovieClip object. For example:

With a clip called 'my_mc' on the main timeline:
var ref:MovieClip = _root.my_mc;
ref._x += 5; // Moves my_mc five pixels right
trave(ref); // Should return '_level0.my_mc'

Object - The default type given to variables not strictly declared. These can be used to hold information associatively, and for other things. For example, MovieClip.getBounds() returns an object with four values which can be accessed.

BitmapData - As the name would imply, stores data for a bitmap in Flash 8 or later. See the appropriate tutorial for more information.


BBS Signature

Response to AS:Variables New 2007-01-28 15:25:31


At 1/28/07 03:18 PM, Paranoia wrote: Hope you don't mind me extending this.

not at all..
the more info we get in this the better!
=)

Response to AS:Variables New 2007-01-28 16:07:46


Hait.


BBS Signature

Response to AS:Variables New 2007-01-28 16:22:04


I'd like to add:

What are variables?
The variable system has been added to flash since flash no.5.They are
usally used to ismplify scripts or to shorten codes.They are very often used with a boolean,true or false.Basicly,they can be anything you want.I sometimes type su=10; instead of speed_up=10;

How can variables be used?
They are mostly used with "onClipEvent(load){"The only difference between onClipEvent(load){ and onClipEvent(enterFrame){ is that onClipEvent(enterFrame){ performs an action every frame,meaning that if the frame rate is one,the action would be performed every frame.
They can be whatever you want as long as each variable has always got the same name.

Excert taken from Toasts wonderful tutorial.

Response to AS:Variables New 2007-01-28 16:27:52


More hait.


BBS Signature

Response to AS:Variables New 2007-01-28 16:32:55


Nice, Read it 3 or 4 times and don't get any of it,

NIce job though seems thorough


I'm cool now... Right guys?... Guys?

Art Thread So I don't Lose It >:(

BBS Signature

Response to AS:Variables New 2007-01-28 18:58:46


At 1/28/07 04:22 PM, Cybex wrote: They are mostly used with "onClipEvent(load){"The only difference between onClipEvent(load){ and onClipEvent(enterFrame){ is that onClipEvent(enterFrame){ performs an action every frame,meaning that if the frame rate is one,the action would be performed every frame.

lol.

I don't care if you are an account stealing impostor; that reference gets you cool points ^.^


BBS Signature

Response to AS:Variables New 2007-02-01 07:19:18


didn't you spell boolean wrong...? wouldn't that be misleading...?

Response to AS:Variables New 2007-02-01 07:28:01


At 2/1/07 07:19 AM, Murch wrote: didn't you spell boolean wrong...? wouldn't that be misleading...?

Pretty much. Just to remind that you should look into just about anything before talking about it from personal experiance.
(It's named after this Bool dude who liked ones and zeroes and calculating stuff with them. So it's pretty much used in computing, except it's more generally called binary logic.)


BBS Signature

Response to AS:Variables New 2007-02-20 08:43:14


To be precise, you didn't really expand on variables. You more of made a compilation of variables, assignment and logical operators, and the if statement. I'm not saying it's an unnecessary topic though, as long as it is titled for what it is.


BBS Signature