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.

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

<strong>to</strong> using a condition. Line 2 of Listing 8.1 shows how we test for the mouse location in relation <strong>to</strong><br />

the box. One property of a movie clip is the mouse location; _xmouse, _ymouse, s<strong>to</strong>res the mouse<br />

location in relation <strong>to</strong> the anchor point for a clip. For this clip the anchor is in the <strong>to</strong>p left corner<br />

<strong>to</strong> make it easier <strong>to</strong> read the code. Another property of a movie clip is its size; _width and _height<br />

s<strong>to</strong>re the dimensions. In this instance if the x or y position of the mouse is greater than zero then<br />

it must be both <strong>to</strong> the right of the left edge and below the <strong>to</strong>p line. If the mouse position is also<br />

less than the <strong>to</strong>tal width and the <strong>to</strong>tal height then it must be within the boundary of the box. We<br />

can combine all these tests in<strong>to</strong> a single test using the logical And symbol &&. Every test needs<br />

<strong>to</strong> evaluate <strong>to</strong> true in order for the combined test <strong>to</strong> evaluate <strong>to</strong> true using this logical opera<strong>to</strong>r.<br />

If the test passes then the code in lines 4 and 5 executes; here we do two things. Line 4 sets the<br />

variable ‘overlap’ <strong>to</strong> ‘Inside’ and moves the frame position of the movie clip ‘Light’ <strong>to</strong> frame 4.<br />

Notice that each of these is prefixed with ‘_parent’. Because this code is inside the movie clip<br />

‘Box’, we need <strong>to</strong> move up our _parent <strong>to</strong> get <strong>to</strong> the main timeline, where the variable ‘overlap’<br />

and the movie clip ‘Light’ are placed. Later in the book we will look at the problems of moving<br />

between different levels of a movie. Sometimes this can be a complex exercise, but hopefully in<br />

this simple example you can see what is going on. If the overlap test failed then we run the code<br />

in Lines 7 and 8 that set the variable ‘overlap’ <strong>to</strong> ‘No overlap’ and the frame for the ‘Light’ movie<br />

clip <strong>to</strong> 1.<br />

One movie clip over another<br />

Another common problem is detecting the overlap of one movie clip over another. A very simple<br />

technique is <strong>to</strong> use the built-in method hitTest.<br />

my_mc.hitTest(x, y, shapeFlag)<br />

my_mc.hitTest(target)<br />

Parameters<br />

x The x coordinate of the hit area on the Stage.<br />

y The y coordinate of the hit area on the Stage.<br />

target The target path of the hit area that may intersect or overlap<br />

with the instance specified by my_mc.<br />

my_mc. A movie clip instance.<br />

shapeFlag A Boolean value specifying whether <strong>to</strong> evaluate the entire<br />

shape of the specified instance (true), or just the bounding<br />

box (false).<br />

Returns<br />

A Boolean value of true if my_mc overlaps with the specified hit area, false otherwise.<br />

Unfortunately hitTest doesn’t give much detail, so we are going <strong>to</strong> develop our own hit<br />

test. Before we study the code let’s try <strong>to</strong> understand the problem. The problem is <strong>to</strong> identify and<br />

classify the overlap of two movie clips. Figure 8.9 shows the possible locations of the two clips.<br />

111

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

Saved successfully!

Ooh no, something went wrong!