<< Chapter < Page Chapter >> Page >

The dragenter event handler.

private function enterHandler(event:DragEvent):void{ if (event.dragSource.hasFormat("imageObject")){DragManager.acceptDragDrop( Canvas(event.currentTarget));} //end if } //end enterHandler

Confirm the correct format string

The code in Listing 13 checks to confirm that the format string in the DragSource object matches "imageObject" (see Listing 11). If so, it calls the static acceptDragDrop method on the DragManager class, passing a reference to itself as a parameter in the method call.

Accept the dragged object

The call to the acceptDragDrop method notifies the DragManager that the Canvas object is willing to accept the contents of the DragSource object being dropped onto itself.

Beginning of the dragDrop event handler

The dragDrop event handler was registered on the Canvas object in Listing 8. This method is executed after the Canvas object accepts the drag and the user releases the mouse button while the drag proxy isover the Canvas .

Correct the drop position for the image

The code in Listing 14 uses the current location of the mouse pointer along with the values stored in localX and localY to compute the new location for the upper-left corner of the image when it is dropped on the canvas.

(Recall that localX and localY contain the coordinates of the mouse pointer relative to the upper-left corner of the image when the mouseDown event was dispatched by the image and the drag and drop operation began.)

I explained the need for this position adjustment earlier .

Beginning of the dragdrop event handler.

private function dropHandler(event:DragEvent):void{ var cornerX:Number = (Canvas(event.currentTarget). mouseX) - localX;var cornerY:Number = (Canvas(event.currentTarget). mouseY) - localY;

Do the drop

Listing 15 checks to confirm that the location at which the upper-left corner of the image will be placed is within the bounds of the canvas on the left sideand the top. If so, it sets the coordinates of the Image object that dispatched the original mouseDown event to the coordinates that were computed in Listing 14. This causes that Image object to move to the new position on the Canvas object.

Do the drop.

if((cornerX>0.0)&&(cornerY>0.0)){ Image(event.dragInitiator).x = cornerX;Image(event.dragInitiator).y = cornerY } //end if} //end dropHandler //--------------------------------------------------//} //end class} //end package

The end of the program

Listing 15 also signals the end of the dragDrop event handler, the end of the Driver class, and the end of the program.

Run the program

I encourage you to run this program from the web. Then copy the code from Listing 16 and Listing 17. Use that code to create a Flex project.Compile and run the project. Experiment with the code, making changes, and observing the results of your changes. Make certain that you can explainwhy your changes behave as they do.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask