13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with arrays<br />

public dynamic class TypedArray ext<strong>en</strong>ds Array<br />

{<br />

private var dataType:Class;<br />

public function TypedArray(typeParam:Class, ...args)<br />

{<br />

dataType = typeParam;<br />

var n:uint = args.l<strong>en</strong>gth<br />

if (n == 1 && (args[0] is Number))<br />

{<br />

var dl<strong>en</strong>:Number = args[0];<br />

var ul<strong>en</strong>:uint = dl<strong>en</strong><br />

if (ul<strong>en</strong> != dl<strong>en</strong>)<br />

{<br />

throw new RangeError("Array index is not a 32-bit unsigned integer ("+dl<strong>en</strong>+")")<br />

}<br />

l<strong>en</strong>gth = ul<strong>en</strong>;<br />

}<br />

else<br />

{<br />

for (var i:int=0; i < n; i++)<br />

{<br />

// type check done in push()<br />

this.push(args[i])<br />

}<br />

l<strong>en</strong>gth = this.l<strong>en</strong>gth;<br />

}<br />

}<br />

}<br />

TypedArray overridd<strong>en</strong> methods<br />

The TypedArray class overrides the four methods of the Array class that are capable of adding elem<strong>en</strong>ts to an array. In<br />

each case, the overridd<strong>en</strong> method adds a type check that prev<strong>en</strong>ts the addition of elem<strong>en</strong>ts that are not the correct data<br />

type. Subsequ<strong>en</strong>tly, each method calls the superclass version of itself.<br />

The push() method iterates through the list of argum<strong>en</strong>ts with a for..in loop and does a type check on each<br />

argum<strong>en</strong>t. Any argum<strong>en</strong>t that is not the correct type is removed from the args array with the splice() method. After<br />

the for..in loop <strong>en</strong>ds, the args array contains values only of type dataType. The superclass version of push() is th<strong>en</strong><br />

called with the updated args array, as the following code shows:<br />

AS3 override function push(...args):uint<br />

{<br />

for (var i:* in args)<br />

{<br />

if (!(args[i] is dataType))<br />

{<br />

args.splice(i,1);<br />

}<br />

}<br />

return (super.push.apply(this, args));<br />

}<br />

The concat() method creates a temporary TypedArray named passArgs to store the argum<strong>en</strong>ts that pass the type<br />

check. This allows the reuse of the type check code that exists in the push() method. A for..in loop iterates through<br />

the args array, and calls push() on each argum<strong>en</strong>t. Because passArgs is typed as TypedArray, the TypedArray<br />

version of push() is executed. The concat() method th<strong>en</strong> calls its own superclass version, as the following code<br />

shows:<br />

Last updated 6/6/2012<br />

46

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!