02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

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>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

Doing things with numbers<br />

Now that you know how <strong>to</strong> create a variable, let’s use that information <strong>to</strong> do some simple<br />

arithmetic. No, don’t groan; images and animation will come soon, but for now let’s concentrate<br />

on easy manipulation of simple variables.<br />

1 var x:Number = 10;<br />

2 var y:Number = 7;<br />

3 trace( x + y);<br />

Listing 7.2<br />

The <strong>ActionScript</strong> in Listing 7.2 will give a result of 17 in the Output window. There are many<br />

operations that you can do using numbers,<br />

Table 7.1 Using symbolic opera<strong>to</strong>rs with x = 10 and y = 7<br />

Operation Description Example<br />

++ Increment x ++results in 11<br />

−− Decrement x −−results in 9<br />

+ Addition x + y results in 17<br />

− Subtraction x − y results in 3<br />

∗ Multiplication x ∗ y results in 70<br />

/ Division x/y results in 1.42857143<br />

% Modulo, remainder after division x%y results in 3<br />

| Or x|y results in 15<br />

& And x&y results in 2<br />

\ XOR x\y results in 13<br />

≪ Shift left x ≪ y results in 1280<br />

≫ Shift right x ≫ y results in 0<br />

+= Add and assign x+ =y sets x <strong>to</strong> 17<br />

−= Subtract and assign x− =y sets x <strong>to</strong> 3<br />

∗= Multiply and assign x∗ =y sets x <strong>to</strong> 70<br />

/ = Divide and assign x/ = y sets x <strong>to</strong> 1.42857143<br />

% = Modulo and assign x% = y sets x <strong>to</strong> 3<br />

|= Or and assign x| =y sets x <strong>to</strong> 15<br />

& = And and assign x& = y sets x <strong>to</strong> 2<br />

∧= Xor and assign x∧ =y sets x <strong>to</strong> 13<br />

≪= Shift left and assign x≪= y sets x <strong>to</strong> 1280<br />

≫= Shift right and assign x≫= y sets x <strong>to</strong> 0<br />

Some opera<strong>to</strong>rs are obvious whereas others need some explanation.<br />

% (modulo) opera<strong>to</strong>r<br />

This returns the remainder after division. In this example 10 divided by 7 gives 1 remainder 3 so<br />

the value returned will be 3.<br />

88

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

Saved successfully!

Ooh no, something went wrong!