<< Chapter < Page Chapter >> Page >

The dragdrop event handler.

//Execute the dragDrop event handler to drop the // object in its new location. Compensate for the// fact that the mouse pointer is not at the // upper-left corner of the object when the drag is// initiated. Don't allow the image to be dragged off // the left side of the canvas or off the top of the// canvas. See more information about this in the // comments at the top.private function dropHandler(event:DragEvent):void{ //Add the drag initiator to the new container.// Note that it is not necessary to first remove it // from its old container.//Also note that the z-axis index is lost in this // operation. When an object is dropped on top of// another object in the new container, it stays on // top regardless of the original z-order of the// two objects. //The original z-order has no meaning when you drag// objects into a canvas from several othr Canvas //event.currentTarget.addChild(event.dragInitiator); //Position the dragInitiator in the Canvas object// based on the mouse coordinates at the drop and // the mouse coordinates relative to the upper-// left corner of the drag initiator at the // start of the drag.//Compute the correct position for the upper-left // corner of the dropped object.//If you attempt to drop an object so that it // protrudes out of the left side or the top of// the canvas, the drag and drop operation is // simply aborted.var cornerX:Number = (Canvas(event.currentTarget). mouseX) - localX;var cornerY:Number = (Canvas(event.currentTarget). mouseY) - localY;if((cornerX>0.0)&&(cornerY>0.0)){ event.dragInitiator.x = cornerX;event.dragInitiator.y = cornerY } //end if} //end dropHandler //--------------------------------------------------//} //end class} //end package

The new code

The only real difference between the code in Listing 12 and the similar event handler in the earlier lesson titled Drag and Drop Basics is the first statement in Listing 12 that adds the dragged object as a child of the Canvas object.

What you learned in the earlier lesson in conjunction with the comments in Listing 12 should suffice and no further explanation of this method should benecessary.

The end of the program

Listing 12 also signals the end of the Driver class, the end of the package, 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 13 and Listing 14. 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.

Resources

I will publish a list containing links to ActionScript resources as a separate document. Search for ActionScriptResources in the Connexions search box.

Complete program listings

Complete listings of the MXML code and the ActionScript code are provided below.

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