Last modified on 29 January 2013, at 15:08

Introduction to ActionScript 2.0/Primitive Data Types

Introduction to ActionScript 2.0
Event Handling Primitive Data Types Arrays

Key concepts:


  • Wrapper classes
  • Literals of primitive data types vs. instances of wrapper classes
  • instanceof and typeof
  • Character indices
  • Substrings
  • Other String methods
  • Scientific notation and hexadecimals
  • Infinity, maximum and minimum values, and NaN
  • More about conversion
  • Static properties and methods
  • Constant variables

After cramming all those basic concepts in your head, you deserve a break! This chapter is all about primitive data types.

What is a wrapper class?Edit

A wrapper is a class that 'wraps' the three primitive data types with properties and methods. There are, unsurprisingly, three wrapper classes, each corresponding to a primitive data type:

  • Number
  • String
  • Boolean

Each class has a constructor function which can be used to initialise it. Initialising a number, string or Boolean value with its constructor but without any arguments will return a blank value, i.e. zero, an empty string and an empty Boolean value respectively:

Code Result
trace(new Number());
trace(new String());
trace(new Boolean());

0

false

Although traced as true, new Boolean() is neither true nor false.

You can also put the literal into the first parameter:

Code Result
trace(new String("Hello world!"));
trace(new Number(123));
trace(new Boolean(true));

Hello world!
123
true

There's a slight difference between a 'string' and a 'string object'. Look at the following example:

Code Result
trace(123 instanceof Number);
trace(new Number(123) instanceof Number);
trace(typeof 123);
trace(typeof new Number(123));

false
true
string
object

instanceof and typeof are special operators. instanceof returns true if the left operand is an instance of the right operand. In this example, 123 is not an instance of Number, but new Number(123) is. typeof returns the data type of the right operand, which can be string, number, boolean, function, movieclip or object (for non-MovieClip objects). The lowercase letters are intentional because this is what typeof returns. In our example, 123 is of the primitive data type number, but new Number(123) is an instance of the Number wrapper class.

Flash converts an instance of the wrapper class into the primitive data and vice versa whenever it is necessary. If we want to manually convert an instance of a wrapper class into a primitive data type, we can use the valueOf() function:

Code Result
trace(new Number(123).valueOf() instanceof Number);

false

To convert the other way, simple call the constructor function.

How can I manipulate strings?Edit

The only string property you need to know is the length property, which shows the number of characters in the string. For example:

Code Result
trace("Hello world!".length);

12

Note that the space is counted.

Another thing you need to know is that the position of a character in a string is called its index. If the index is a positive number, it is counted from the first character, and if the index is a negative number, it is counter from the last character. The first character is defined as 0 and the last character is defined as -1. For example, the index of 'p' in 'spot' is 1 or -3 and 'o' is 2 or -2. Now let's start manipulating strings!

How can I locate a character in a string?Edit

The following functions are related to characters in ActionScript:

  • String.fromCharCode(code point 1, code point 2...)
  • string.charAt(index of the character);
  • string.charCodeAt(index of the character);

fromCharCode returns a string using the Unicode code points specified in the parameters. Note that we wrote 'String.fromCharCode'. That's exactly the way you're supposed to type it! This is because fromCharCode is a static method, which means it belongs to the whole class, and not just a specific string.

charAt returns a character at the desired position, while charCodeAt returns the Unicode code point of the desired character.

For example:

Code Result
trace(String.fromCharCode(62, 56, 123));
trace("Hello world!".charAt(4));
trace("Hello world!".charCodeAt(4));

>8{
o
111

Remember, ActionScript uses Unicode code points, not ASCII ones, for string manipulation. ASCII code points will come in handy later.

What is a substring?Edit

A substring is a string inside another string. There are three ways to find a substring when we know its position in a string:

  • string.substr(index of the first character, length);
  • string.substring(index of the first character[, index of the last character]);
  • string.slice(index of the first character[, index of the last character]);

If you are familiar with SQL or spreadsheets, you may have noticed find that these two functions are quite different from functions like SUBSTRING()/SUBSTR()/MID() in SQL or spreadsheets as the string is not specified in the first parameter because it is the string that contains the method in the first place.

Note that the the last character is excluded from the substring in substring() and slice(). If the index of the last character is not specified, Flash will return everything till the end. Here's an example with all three functions:

Code Result
trace("Hello world!".substr(4, 5));
trace("Hello world!".slice(4, 9));
trace("Hello world!".substring(4, 9));
trace("Hello world!".slice(4));
trace("Hello world!".substring(4));

o wor
o wor
o wor
o world!
o world!

Sometimes, we need to find the position of a known substring inside a string. Then we need these two functions:

  • string.indexOf(substring[, index of the first character]);
  • string.lastIndexOf(substring[, index of the last character]);

indexOf is very similar to FIND() in spreadsheets. It looks for the substring in the string starting from the character specified in the second parameter. If the second parameter is left blank, it starts from the beginning. The position of the first character of the first occurrence of the substring is returned. lastIndexOf is pretty much the same except it returns the last occurrence, not the first, and you specify when to stop looking, not where to start looking. The search is case-sensitive. If no occurence is found, Flash returns -1. Example:

Code Result
trace("Tralalalala".indexOf("la", 4));
trace("Tralalalala".indexOf("la"));
trace("Tralalalala".lastIndexOf("la", 8));
trace("Tralalalala".lastIndexOf("la"));
trace("Tralalalala".lastIndexOf("lah"));

5
3
7
9
-1


What are some other String methods?Edit

  • toString(subject of conversion) is an alternative way to convert something to a string. It's pretty much the same as String(subject of conversion), which we've met back in Chapter 1.
  • concat(string1, string2...) also does pretty much the same thing as the text concatenation operator.
  • string.toUpperCase and string.toLowerCase converts the specified string to upper- and lowercase respectively. Surprising, eh?
  • string.split(delimiter, number of elements) will be covered in the next chapter.

How can I start a new line?Edit

newline is a string literal that can help you start a new line. It is not enclosed with quotation marks; instead, it is concatenated with other strings.

Code Result
trace("Hello!" + newline + "World!");

Hello!
World!

How can I express a number in other notation systems?Edit

ActionScript represents numbers in scientific notation in a way that is quite similar to calculators. The letter 'e' is added between the 10x portion and the number. You don't have to ensure that the first number is between 1 and 10, or add a positive (+) sign before the exponent, as Flash automatically does that for you. Flash also converts numbers to and from scientific notation when it see fits:

Code Result
trace(123e15);
trace(1.23e-2);
trace(10000000000000000000000000000000000);

1.23e+17
0.0123
1e+34

To express a number o hexadecimals, simply type in '0x' before the hexadecimal. Flash will convert it into denary for you:

Code Result
trace(0x12A);

298

Hexadecimals will be very useful when we deal with colours later.

What are some special numbers?Edit

Apart from the usual floating-point decimals and integers we manipulate, ActionScript has five special numbers, all static properties of Number:

  • Number.NEGATIVE_INFINITY
  • Number.POSITIVE_INFINITY
  • Number.MAX_VALUE
  • Number.MIN_VALUE
  • Number.NaN

Like a static method, a static property is a property that belongs to the whole class, not a single instance. They are also accessed using the class name rather than the instance name. Note that these numbers are in all caps and separated by underscores (_). This is because they are constants. Although constants are variables, they are not supposed to be changed; they're just there because it would be infeasible to use literals every time.

The literals of Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY and Number.NaN are -Infinity, Infinity and NaN respectively.

Number.MAX_VALUE and Number.MIN_VALUE are the largest integral value and smallest decimal value in Flash respectively. Number.NEGATIVE_INFINITY and Number.POSITIVE_INFINITY represent, well, negative and positive infinity respectively. Number.NaN means 'not a number'. You've probably got this value before, when you tried to treat non-numbers as numbers, such as the following:

Code Result
trace(123 - "Hello world!");

NaN

Remember that infinity is always infinity, no matter how much you subtract from it or into how many pieces you divide it. Also remember that dividing by zero does not result in undefined or NaN, but infinity.

Code Result
trace(Number.POSITIVE_INFINITY / Number.MAX_VALUE);
trace(Number.POSITIVE_INFINITY - Number.MAX_VALUE);
trace(12 / 0);

Infinity
Infinity
Infinity

Two global functions, isFinite(number) and isNaN(number), can help you decide whether a number is inFinite and NaN respectively. isFinite returns true if the number is finite and false if it isn't; isNaN returns false if the parameter is a legitimate number and otherwise returns true.

Code Result
trace(isFinite(Infinity));
trace(isFinite(-Infinity));
trace(isFinite(12/0));
trace(isFinite(Number.MAX_VALUE));
trace(isNaN(45 * "Hello world!"));
trace(isNaN("Hello world!"));
trace(isNaN(-12/0));

false
false
false
true
true
true
true

Note that the last one is true because "Hello world!" is not a number (but a string). We will go into even more detail in the Math class chapter.

A word on conversionEdit

Back in the first chapter, we learnt to convert between the primitive data types. Let's take another look at each conversion process.

Let's start with String(value). If we attempt to convert a MovieClip or TextField, it will be converted into the form 'leveln_instanceName', e.g. level0_someMovieClip refers to a MovieClip on the main timeline. If we attempt to convert an array, the resulting string will contain all the elements separated by a comma. If we attempt to convert any other object, we will get [object Object], and if we attempt to convert a function, we get [type Function].

Code Result
//Assume that someMovieClip is a MovieClip on the stage.
trace(String(new Array(12, 32, 34)));
trace(String(someMovieClip));
trace(String(new Sound));
trace(String(someFunction));
function someFunction():Void{
	return;
}

12,32,34
_level0.someMovieClip
[object Object]
[type Function]

If we put anything other than true, false or a numeral-only string into Number(value), we would get NaN.

If we put anything other than 0 or 1 into Boolean(value), we would get false if that anything is null or undefined, and true otherwise.

Another way to convert a string into an integer is to use the parseInt global function. The first parameter is a string, and the second, optional parameter is the base of the numeral system.

Code Result
trace(parseInt("13.6"));
trace(parseInt("0x14"));
trace(parseInt("1.23e-3"));
trace(parseInt("001101", 2));

13
20
1
13

Note that we don't need a second parameter for the second trace because 0x already implies that the base is 16.

ConclusionEdit

Now that we've learnt all about primitive data types, we will learn how to manipulate arrays.

NotesEdit