Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AS3 Basics By Ampy
- -------------------------------
- VARIABLES
- The purpose of a variable is to store data an example of this would be as shown below:
- var myvar:String = "hello!";
- Now I'm going to break down each part of it:
- var - This basically tells flash you want to create a new variable. It's a neccessity.
- myvar- This does not have to be myvar and I do not recommend using that name. It was an example.
- It can be ANYTHING you want it to be, it's typical to name it based on what your using
- it for so you can keep track of things
- : - This is neccessary, you just have to do it.
- String - This is the datatype I chose, their are various data-types in AS3, I'm going to list
- some of them below.
- DATATYPES
- -----------------------------------------
- String - Any words/text
- EXAMPLE: var hi:String = "Sup";
- Boolean - This can be equal to true or false
- EXAMPLE: var bool:Boolean = false;
- If you don't set it equal to anything, by default it's false
- int - An integer
- EXAMPLE: var myInteger:int = 3;
- Number - A Number, int is recommended over this due to speed/efficiency
- EXAMPLE: var myNum:Number = 7;
- Sprite - Basically a Image
- EXAMPLE: var spr:Sprite = new Sprite();
- It's like an image, so therefore you have to create a new version or instance of it
- Array - Holds multiple variables or values
- EXAMPLE: var arr:Array = new Array("hi", "sup", "yay", "wat", "wut");
- It's basically multiple variables, I'll go over have to access them in the
- Array section.
- I'm not going to explain every single one you'll have to do some studying but I
- will list some of them below:
- TextField
- MovieClip
- Object
- Vector
- URLVariables
- URLRequest
- URLLoader
- That's not all of them, but not gonna list all of them.
- -----------------------------------------------
- ARRAYS
- Okay so I already explained how to use/create a array, now I'm going to explain
- how to access them. Say I have this line of code:
- var arr:Array = new Array("hi", "wat", "wut", "k");
- This could be accessed both the following ways:
- arr[0];
- Each one in the array has a specific number or index. Which goes up by 1 for each one
- but starting at 0, so:
- arr[0] - "hi"
- arr[1] - "wat"
- arr[2] - "wut"
- arr[3] = "k"
- A Full EXAMPLE:
- var arr:Array = new Array("hi", "k", "y");
- trace(arr[0]);
- I know I haven't been over this yet, but what trace, does it output something to the output
- panel in flash. Here arr[0] has a stored value of "hi". So it would output "hi" in flash.
- ----------------------------------------
- IF STATEMENTS
- Okay so if statements are pretty much self explanatory. If this is true or has happened do this.
- Here's an example:
- var str:String = "hi";
- if(str == "hi")
- {
- trace("str is equal to hi");
- }
- You'll notice that I didn't use = I used ==, this is an operator in flash. I'll list operators
- along with their definition below:
- == - Is equal to
- != - is Not equal to
- < - is less than
- > - is greater than
- <= - is less than or equal to
- >= - is greater than or equal to
- + - Addition
- += - Plus the current value of what your adding
- - Subtraction
- -= Minus the current value of what your subtracting
- * - Multiply
- *= Multiply the current value of what your multiplying
- / - Division
- /= Divide the current value of what your dividing
- Their's a few more but these are all you need to know for now. So a basic math example:
- var myInt:int = 3;
- if(myInt < 3)
- {
- trace("myInt is less than 3");
- } else {
- trace("myInt is not less than 3");
- Notice I used else here. It should be self explanatory if it isn't ask me.
- Now I'm going to go over else if even though I shouldn't really, but whatever.
- var myInt:int = 6;
- if(myInt == 6)
- {
- trace("equals 6");
- } else if(myInt > 6) {
- trace("is greater than 6");
- Basically else if is saying if this isn't true, then if this is true try this.
- -----------------------------------------------------
- EVENT LISTENERS
- Okay so this is getting to the cooler stuff event listeners. Making buttons work and stuff.
- Here's an example of making a button work.
- instance_name.addEventListener(MouseEvent.CLICK, function_name);
- Let's say in place of function name I have buttonClicked.
- FULL CODE:
- instance_name.addEventListener(MouseEvent.CLICK, buttonClicked);
- function buttonClicked(e:MouseEvent):void
- {
- gotoAndStop(2);
- }
- Okay so what I did here is add an event listener. You'll have to study all of the events
- I don't even know ALL of them myself.
- Next I declared a function, and then the function name which is after the comma in the event listener
- You'll get errors if the function names not the same. Next in the parantheses I said e, that can be
- anything you want, but it's typical to use e or event their to keep track of things. Next you always
- use colon :, then the event you used in the event listener which in this case is MouseEvent. After that
- close parantheses, and :void, this isn't neccessary in some concerns but I treat it mandatory or neccessary.
- Because 99% of the time you'll be using it.
- What gotoAndStop(2); is doing is telling flash to goto and stop at frame 2. If you don't have a frame inserted on the
- timeline it won't go there, because flash has no clue what frame to go to because it theoretically doesn't
- exist. 2 can be any frame of course, I was using 2 as an example however.
- ---------------------------------------------------------------
- LOOPS
- Loops are used to execute code repeatedly without having to use thousands of lines
- to do one thing. Their's several types of loops.
- FOR LOOP
- EXAMPLE:
- var myInt:int = 50;
- for(var i:int = 0; i < myInt; i++)
- {
- trace("Is less than");
- }
- I declared a variable called myInt and gave it value of 50. A for loop is initialized or started
- by using the keyword for then parentheses. In their I created a variable named i and gave it a value
- 0. I used i because it is the most COMMON in for loops. Then I said without using if. If i is less than
- myInts value. Increment i's value or go up by one.
- Since I didn't cover incrementing this is the only 2 ways:
- ++ - Goes up by 1 each time
- -- Goes down by 1 each time
- Back to the for loop example now. Everytime i is still less than myInt, trace or output "Is less than"
- to flashs output panel until it reaches i's value.
- DO LOOP
- A do loop makes whatever your doing true no matter what so..
- var myInt:int = 3;
- do {
- myInt++;
- }
- That's basic usage.
- WHILE LOOP
- A while loop is basically saying while this is this do this, so heres an example:
- var myInt:int = 5;
- while(myInt > 0)
- {
- do {
- myInt--;
- }
- }
- Their's a few more but their a little more advanced so I'm not going to go over them yet
- , but here's a full example of the 3 loops I showed you in this section:
- var arr:Array = new Array("hi", "bob", "todd", "y, "k");
- var myInt:int = 0;
- for(var i:int = 0; i < arr.length; i++)
- {
- trace("arr's length " + arr.length);
- while(i < arr.length)
- {
- var tf:TextField = new TextField();
- tf.text = "Text!";
- addChild(tf);
- }
- do {
- myInt++;
- }
- OKay so here I created an array and with a for loop, said if i less than the number of values
- in the array. trace "arr's length" plus it's actual length by using the length property.
- Then I said while i is stil less than arrs length, create a new textfield and I gave it text of
- "Text!";
- Then what add child does is add it to the stage and make it visible to you
- Then the do loop executes code immediately and increments myInt's value.
- --------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement