ActionScript Programming/PartI/Chapter 3/Objects, Scopes

String.split (method)
String.split(, [limit] );

This function splits the specified String into substrings by breaking it wherever the specified parameter occurs, and returns the substrings in an array. For example you have a string of values separated with commas. By using this method you can separate this values and import them into an array. This example illustrates how to do that:

1.  var tmpString = new String("Mario,Henry,George,Amy,Kate"); 2.  var tmpValues = new Array; 3.  tmpValues = tmpString.split(","); 4.  trace(tmpValues[0]); 5.  trace(tmpValues[1]); 6.  trace(tmpValues[2]); 7.  trace(tmpValues[3]); 8.  trace(tmpValues[4]);

By specifying [limit] parameter you can limit the number of values. For example if we set the parameter [limit] in the previous example to 2 then the length of the array will be 2 and only first two values: “Mario” and “Henry” will be traced.

Objects
Nearly everything in Macromedia Flash is an object. For example all data types are objects. Now lets see what we can do with objects. First of all by declaring an object variable we can then assign to it any other data type. Example:

1.  var obj = new Object; 2.  var num1 = new Number(20); 3.  var num2 = new Number(30); 4.  var str1 = new String("50"); 5.  var str2 = new String("70"); 6.  trace("str1 + str2 = " + (str1 + str2)); 7.  trace("num1 + num2 = " + (num1 + num2)); 8.  obj = num1; //Now obj is number 9.  obj += num2; 10. trace("obj = " + obj); 11. obj = str1; //Now obj is string 12. obj += str2; 13. trace("obj = " + obj);

In line 1 we declare an object variable named “obj”. In lines 2-3 we declare a number variables “num1” and “num2” and correspondingly assign them values 20 and 30. //output // strTemp = ["this", "is", "a", "string", "to", "array", "test"] private function stringToArray($string:String):Array {      var a = $string.split(" "); var temp = []; for (var x = 0; x < a.length; x++) { temp.push(a[x]); }      return temp; }

Scopes of variables
In this chapter you learned how to declare, initialize and use variables, but we didn’t talk about the variables limitations. There are three groups of variables: global variables, timeline variables and local variables. The global variables are accessible everywhere in the movie, the timeline variables are accessible from any timeline, and the local variables can be only accessed from the functions they are declared in (functions are like events, you will learn about them later). Now lets understand each of this groups in more details.

Global variables
First of all lets understand what means the phrase “the global variables are accessible everywhere in the movie”. As you know the flash document contain scenes and may contain other objects like movie clips, buttons, pictures, etc. By declaring a global variable it can be used everywhere listed above or in short everywhere in the movie (flash document). For the demonstration lets do the following: create a new “movie clip” (for doing it just draw something in the scene, select it and press “F8”. When “Convert to Symbol” dialog appear type the name of the “movie clip”, select “Movie Clip” as behavior and press “OK”) and make sure it is added to the scene (if it isn’t press F11 to see the “library” window and drag your “movie clip” to the scene. Then open the actions of the first frame of the scene and write the following code:

var MyVar = new String("Hello!!!");

Now double click the “movie clip” you have created for navigating to it and open its first frame’s actions and write the following:

trace(MyVar);

Now play the movie. The output will be:



This example demonstrated that the variable which was declared in the timeline cannot be accessed from other objects like “movie clip”. To avoid this we can declare our variable as global. Declaring global variables are like declaring a usual variables except two things: 1) do no write “var” at the beginning of the declaration and 2) append “_global” text at the beginning of the variable’s name. Example:

_global.MyVar = new String("Hello!!!");

Now if you test the following example once more, but this time declare “MyVar” variable as global it will work great.

Timeline variables
This group of variables are accessible everywhere in the timeline. This means that if you declare a variable in frame 1, it can be accessed from all other frames and scenes, in short from everywhere in the timeline. Declaring timeline variables are like declaring a usual variables except writing the keyword “var” at the beginning of the declaration. Example:

MyVar = new String("Hello!!!");

Local variables
This group of variables are only accessible from the functions (or block of code) they are declared in. You already know declaring a local variables. Example:

var MyVar = new String("Hello!!!");

For understanding what “block of code” is lets do the following: create a new flash document, open the first frame’s actions and write the following:

var MyVar = new String("Hello!!!");

Then create a button (for doing it just draw your button, select it, press F8, and when “Convert to Symbol” dialog appear enter the button’s name, select “Button” as a behavior and press “OK”), open its actions and write the following:

on (release) { trace(MyVar); }

Now test the movie (tip: just press “Ctrl+Enter” for doing that). If you now press the button the output will be: “Hello!!!”. What has happened? Isn’t “MyVar” a local variable? The answer is yes, it is local variable. The cause that it is accessible from the button is that it is declared in the frame. By writing anything in frame’s actions it is made a part of “_root” object and as all frames are part of “_root” object they “see” each other. This means that if you declare a local variable in any frame, then it is made a local variable of “_root” object, so it can be accessed from any frame and especially button, located on that frame (all object located on timeline are members of “_root” object). So all this means that a local variable, declare in timeline, is the same as a timeline variable.

« Previous &emsp;&emsp; Next »