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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

}<br />

return false;<br />

Chapter 8: In tip-<strong>to</strong>p condition<br />

This little function returns true if the point (x, y) is inside the rectangle (left, <strong>to</strong>p, right, bot<strong>to</strong>m).<br />

To use the function in the first example we would use:<br />

if (pointInRect(clip._x, clip._y, 100, 200, 300, 400)){<br />

//Do something<br />

}<br />

‘Examples/Chapter08/Conditions02.fla’ shows this function in action.<br />

But if we want <strong>to</strong> run the code if the character is inside the rectangle (100, 200, 300, 400) or<br />

the rectangle (400, 25, 500, 127) then we can combine two calls <strong>to</strong> the function using:<br />

if (pointInRect(clip._x, clip._y, 100, 200, 300, 400) ||<br />

pointInRect(clip._x, clip._y, 400, 25, 500, 127)){<br />

//Do something<br />

}<br />

The symbol ‘||’ is the logical Or, if one side or the other of the symbol is true then the combined<br />

result is true. Notice how <strong>Flash</strong> allows you <strong>to</strong> put a long line on<strong>to</strong> two or more lines; <strong>Flash</strong> ignores<br />

what is referred <strong>to</strong> as ‘white space’ which includes spaces and carriage returns, so you can fit the<br />

code in<strong>to</strong> a narrower text box.<br />

Let’s try out a more colourful example<br />

When you are learning about using any sort of<br />

programming language there is no substitute for<br />

hands-on experience. We are going <strong>to</strong> look at a<br />

practical example of the ‘if ’ statement in action and<br />

you are firmly encouraged <strong>to</strong> run your copy of <strong>Flash</strong><br />

and open ‘Examples\Chapter08\Conditions03.fla’.<br />

Take a look at the _root level timeline. There are<br />

five layers; each layer has been named with a relevant<br />

name. Most of the code we are going <strong>to</strong> study<br />

is in the layer ‘Control’ on frame 1. When declaring<br />

new functions for a movie clip or _root level<br />

timeline, frame 1 is a good place <strong>to</strong> put the new<br />

function code. In this example we will be using a<br />

function that is placed on the first frame.<br />

The purpose of the program is <strong>to</strong> detect when<br />

Figure 8.2 The layers used in the<br />

Conditions03 project<br />

the mouse arrow overlaps with the box in the middle of screen. As well as detecting the overlap,<br />

107

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

Saved successfully!

Ooh no, something went wrong!