SHARE
TWEET

Untitled

a guest Aug 12th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. AS3 Basics By Ampy
  2. -------------------------------
  3.  
  4. VARIABLES
  5.  
  6.     The purpose of a variable is to store data an example of this would be as shown below:
  7.    
  8.         var myvar:String = "hello!";
  9.        
  10.     Now I'm going to break down each part of it:
  11.    
  12.         var - This basically tells flash you want to create a new variable. It's a neccessity.
  13.        
  14.         myvar- This does not have to be myvar and I do not recommend using that name. It was an example.
  15.                It can be ANYTHING you want it to be, it's typical to name it based on what your using
  16.                it for so you can keep track of things
  17.                
  18.         : - This is neccessary, you just have to do it.
  19.        
  20.         String - This is the datatype I chose, their are various data-types in AS3, I'm going to list
  21.                  some of them below.
  22.                  
  23.                  DATATYPES
  24.                  -----------------------------------------
  25.                  
  26.                  String - Any words/text
  27.                  
  28.                     EXAMPLE: var hi:String = "Sup";
  29.                  
  30.                  Boolean - This can be equal to true or false
  31.                  
  32.                     EXAMPLE: var bool:Boolean = false;
  33.                    
  34.                         If you don't set it equal to anything, by default it's false
  35.                        
  36.                  int - An integer
  37.                  
  38.                     EXAMPLE: var myInteger:int = 3;
  39.                    
  40.                  Number - A Number, int is recommended over this due to speed/efficiency
  41.                  
  42.                     EXAMPLE: var myNum:Number = 7;
  43.                    
  44.                  Sprite - Basically a Image
  45.                  
  46.                    
  47.                     EXAMPLE: var spr:Sprite = new Sprite();
  48.                    
  49.                         It's like an image, so therefore you have to create a new version or instance of it
  50.                        
  51.                  Array - Holds multiple variables or values
  52.                  
  53.                     EXAMPLE: var arr:Array = new Array("hi", "sup", "yay", "wat", "wut");
  54.                    
  55.                         It's basically multiple variables, I'll go over have to access them in the
  56.                         Array section.
  57.                        
  58.         I'm not going to explain every single one you'll have to do some studying but I
  59.         will list some of them below:
  60.        
  61.             TextField
  62.             MovieClip
  63.             Object
  64.             Vector
  65.             URLVariables
  66.             URLRequest
  67.             URLLoader
  68.            
  69.             That's not all of them, but not gonna list all of them.
  70.            
  71. -----------------------------------------------
  72. ARRAYS
  73.  
  74.         Okay so I already explained how to use/create a array, now I'm going to explain
  75.         how to access them. Say I have this line of code:
  76.        
  77.             var arr:Array = new Array("hi", "wat", "wut", "k");
  78.            
  79.             This could be accessed both the following ways:
  80.            
  81.                 arr[0];
  82.                
  83.                 Each one in the array has a specific number or index. Which goes up by 1 for each one
  84.                 but starting at 0, so:
  85.                
  86.                 arr[0] - "hi"
  87.                 arr[1] - "wat"
  88.                 arr[2] - "wut"
  89.                 arr[3] = "k"
  90.                
  91.                 A Full EXAMPLE:
  92.                
  93.                 var arr:Array = new Array("hi", "k", "y");
  94.                    
  95.                     trace(arr[0]);
  96.                    
  97.                    
  98.                 I know I haven't been over this yet, but what trace, does it output something to the output
  99.                 panel in flash. Here arr[0] has a stored value of "hi". So it would output "hi" in flash.
  100.                
  101. ----------------------------------------
  102. IF STATEMENTS
  103.  
  104.         Okay so if statements are pretty much self explanatory. If this is true or has happened do this.
  105.         Here's an example:
  106.        
  107.         var str:String = "hi";
  108.        
  109.             if(str == "hi")
  110.             {
  111.            
  112.                 trace("str is equal to hi");
  113.                
  114.             }
  115.            
  116.         You'll notice that I didn't use = I used ==, this is an operator in flash. I'll list operators
  117.         along with their definition below:
  118.        
  119.         == - Is equal to
  120.         != - is Not equal to
  121.         < - is less than
  122.         > - is greater than
  123.         <= - is less than or equal to
  124.         >= - is greater than or equal to
  125.         + - Addition
  126.         += - Plus the current value of what your adding
  127.         - Subtraction
  128.         -= Minus the current value of what your subtracting
  129.         * - Multiply
  130.         *= Multiply the current value of what your multiplying
  131.         / - Division
  132.         /= Divide the current value of what your dividing
  133.        
  134.         Their's a few more but these are all you need to know for now. So a basic math example:
  135.        
  136.         var myInt:int = 3;
  137.        
  138.             if(myInt < 3)
  139.             {
  140.            
  141.                 trace("myInt is less than 3");
  142.                
  143.                 } else {
  144.                
  145.                     trace("myInt is not less than 3");
  146.                    
  147.         Notice I used else here. It should be self explanatory if it isn't ask me.
  148.  
  149.         Now I'm going to go over else if even though I shouldn't really, but whatever.
  150.        
  151.         var myInt:int = 6;
  152.        
  153.             if(myInt == 6)
  154.             {
  155.            
  156.                 trace("equals 6");
  157.                
  158.                 } else if(myInt > 6) {
  159.                
  160.                     trace("is greater than 6");
  161.                    
  162.         Basically else if is saying if this isn't true, then if this is true try this.
  163.  
  164. -----------------------------------------------------
  165. EVENT LISTENERS
  166.  
  167.     Okay so this is getting to the cooler stuff event listeners. Making buttons work and stuff.
  168.     Here's an example of making a button work.
  169.    
  170.         instance_name.addEventListener(MouseEvent.CLICK, function_name);
  171.        
  172.             Let's say in place of function name I have buttonClicked.
  173.            
  174.         FULL CODE:
  175.  
  176.         instance_name.addEventListener(MouseEvent.CLICK, buttonClicked);
  177.        
  178.         function buttonClicked(e:MouseEvent):void
  179.         {
  180.        
  181.             gotoAndStop(2);
  182.            
  183.         }
  184.  
  185.         Okay so what I did here is add an event listener. You'll have to study all of the events
  186.         I don't even know ALL of them myself.
  187.        
  188.         Next I declared a function, and then the function name which is after the comma in the event listener
  189.         You'll get errors if the function names not the same. Next in the parantheses I said e, that can be
  190.         anything you want, but it's typical to use e or event their to keep track of things. Next you always
  191.         use colon :, then the event you used in the event listener which in this case is MouseEvent. After that
  192.         close parantheses, and :void, this isn't neccessary in some concerns but I treat it mandatory or neccessary.
  193.         Because 99% of the time you'll be using it.
  194.        
  195.         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
  196.         timeline it won't go there, because flash has no clue what frame to go to because it theoretically doesn't
  197.         exist. 2 can be any frame of course, I was using 2 as an example however.
  198.        
  199. ---------------------------------------------------------------
  200. LOOPS
  201.  
  202.             Loops are used to execute code repeatedly without having to use thousands of lines
  203.             to do one thing. Their's several types of loops.
  204.            
  205.             FOR LOOP
  206.            
  207.             EXAMPLE:
  208.            
  209.             var myInt:int = 50;
  210.            
  211.                 for(var i:int = 0; i < myInt; i++)
  212.                 {
  213.                
  214.                     trace("Is less than");
  215.                    
  216.                 }  
  217.                
  218.             I declared a variable called myInt and gave it value of 50. A for loop is initialized or started
  219.             by using the keyword for then parentheses. In their I created a variable named i and gave it a value
  220.             0. I used i because it is the most COMMON in for loops. Then I said without using if. If i is less than
  221.             myInts value. Increment i's value or go up by one.
  222.  
  223.             Since I didn't cover incrementing this is the only 2 ways:
  224.            
  225.             ++ - Goes up by 1 each time
  226.             --  Goes down by 1 each time
  227.            
  228.             Back to the for loop example now. Everytime i is still less than myInt, trace or output "Is less than"
  229.             to flashs output panel until it reaches i's value.
  230.            
  231.             DO LOOP
  232.            
  233.             A do loop makes whatever your doing true no matter what so..
  234.            
  235.             var myInt:int = 3;
  236.            
  237.                 do {
  238.                
  239.                 myInt++;
  240.                
  241.                 }
  242.                
  243.             That's basic usage.
  244.  
  245.             WHILE LOOP
  246.            
  247.             A while loop is basically saying while this is this do this, so heres an example:
  248.            
  249.                 var myInt:int = 5;
  250.                
  251.                     while(myInt > 0)
  252.                     {
  253.                    
  254.                         do {
  255.                        
  256.                         myInt--;
  257.                        
  258.                         }
  259.                     }
  260.  
  261.             Their's a few more but their a little more advanced so I'm not going to go over them yet
  262.             , but here's a full example of the 3 loops I showed you in this section:
  263.            
  264.                 var arr:Array = new Array("hi", "bob", "todd", "y, "k");
  265.                 var myInt:int = 0;
  266.                
  267.                     for(var i:int = 0; i < arr.length; i++)
  268.                     {
  269.                    
  270.                         trace("arr's length " + arr.length);
  271.                        
  272.                         while(i < arr.length)
  273.                         {
  274.                        
  275.                             var tf:TextField = new TextField();
  276.                             tf.text = "Text!";
  277.                             addChild(tf);
  278.                            
  279.                         }
  280.  
  281.                             do {
  282.                            
  283.                                 myInt++;
  284.                                
  285.                             }
  286.                            
  287.             OKay so here I created an array and with a for loop, said if i less than the number of values
  288.             in the array. trace "arr's length" plus it's actual length by using the length property.
  289.  
  290.             Then I said while i is stil less than arrs length, create a new textfield and I gave it text of
  291.             "Text!";
  292.            
  293.             Then what add child does is add it to the stage and make it visible to you
  294.             Then the do loop executes code immediately and increments myInt's value.
  295.            
  296. --------------------------------------------------------------------------------------------
RAW Paste Data
Top