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

Create successful ePaper yourself

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

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

Display programming<br />

// This code creates a mouse drag interaction using the startDrag()<br />

// technique.<br />

// square is a MovieClip or Sprite instance).<br />

import flash.ev<strong>en</strong>ts.MouseEv<strong>en</strong>t;<br />

// This function is called wh<strong>en</strong> the mouse button is pressed.<br />

function startDragging(ev<strong>en</strong>t:MouseEv<strong>en</strong>t):void<br />

{<br />

square.startDrag();<br />

}<br />

// This function is called wh<strong>en</strong> the mouse button is released.<br />

function stopDragging(ev<strong>en</strong>t:MouseEv<strong>en</strong>t):void<br />

{<br />

square.stopDrag();<br />

}<br />

square.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.MOUSE_DOWN, startDragging);<br />

square.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.MOUSE_UP, stopDragging);<br />

This technique suffers from one fairly significant limitation: only one item at a time can be dragged using<br />

startDrag(). If one display object is being dragged and the startDrag() method is called on another display object,<br />

the first display object stops following the mouse immediately. For example, if the startDragging() function is<br />

changed as shown here, only the circle object will be dragged, in spite of the square.startDrag() method call:<br />

function startDragging(ev<strong>en</strong>t:MouseEv<strong>en</strong>t):void<br />

{<br />

square.startDrag();<br />

circle.startDrag();<br />

}<br />

As a consequ<strong>en</strong>ce of the fact that only one object can be dragged at a time using startDrag(), the stopDrag()<br />

method can be called on any display object and it stops whatever object is curr<strong>en</strong>tly being dragged.<br />

If you need to drag more than one display object, or to avoid the possibility of conflicts where more than one object<br />

might pot<strong>en</strong>tially use startDrag(), it’s best to use the mouse-following technique to create the dragging effect. With<br />

this technique, wh<strong>en</strong> the mouse button is pressed, a function is subscribed as a list<strong>en</strong>er to the mouseMove ev<strong>en</strong>t of the<br />

Stage. This function, which is th<strong>en</strong> called every time the mouse moves, causes the dragged object to jump to the x, y<br />

coordinate of the mouse. Once the mouse button is released, the function is unsubscribed as a list<strong>en</strong>er, meaning it is<br />

no longer called wh<strong>en</strong> the mouse moves and the object stops following the mouse cursor. Here is some code that<br />

demonstrates this technique:<br />

Last updated 6/6/2012<br />

175

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

Saved successfully!

Ooh no, something went wrong!