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!