<< Chapter < Page Chapter >> Page >

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 for the MXML and ActionScript code discussed in this lesson are provided in Listing 16 and Listing 17 below.

The mxml file for draganddrop01.

<?xml version="1.0" encoding="utf-8"?><!--DragAndDrop01 --><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"xmlns:cc="CustomClasses.*"><cc:Driver/></mx:Application>
Listing

The actionscript file for draganddrop01.

/*DragAndDrop01 Illustrates the fundamentals of drag and drop inActionScript 3. Places three images in a Canvas object:0 - space.jpg - largest 1 - snowscene.jpg - midsize2 - frog.jpg - smallest Sets the z-axis indices as shown above. This causes thespace image to be in the back, the frog image to be in the front, and the snowscene image to be in the middle.Any of the images can be dragged and dropped anywhere within the canvas so long as the mouse pointer doesn'tleave the canvas. However, If the edge of the dragged image goes outside the left edge or the top of the canvas,the drag and drop operation is aborted. If the dragged image goes outside the right side or the bottom of thecanvas, scroll bars automatically appear on the canvas. The size of the canvas is based on the size of the spaceimage so that other images can be substituted for mine when the program is recompiled so long as the file namesand paths are the same. *********************************************************/package CustomClasses{ import flash.events.MouseEvent;import mx.containers.Canvas;import mx.controls.Image; import mx.core.DragSource;import mx.events.DragEvent; import mx.events.FlexEvent;import mx.managers.DragManager; //====================================================//public class Driver extends Canvas {private var imageA:Image = new Image(); private var imageB:Image = new Image();private var imageC:Image = new Image(); private var localX:Number;private var localY:Number;public function Driver(){//constructor //Make the Canvas visible.setStyle("backgroundColor",0x00FFFF); setStyle("backgroundAlpha",1.0);//Embed the image files in the SWF file. [Embed("snowscene.jpg")]var imgA:Class;[Embed("space.jpg")] var imgB:Class;[Embed("frog.jpg")]var imgC:Class;//Load the images from the embedded image files // into the Image objects.imageA.load(imgA); imageB.load(imgB);imageC.load(imgC);// Set the z-axes indices such that the frog is // in front, the snowscene is in the middle and the// space image is at the back. addChildAt(imageB,0);//set index to 0addChildAt(imageA,1);//set index to 1 addChildAt(imageC,2);//set index to 2//Register an event handler that will be executed // whcn the canvas and its children are fully// constructed. this.addEventListener(FlexEvent.CREATION_COMPLETE,completeHandler); } //end constructor//--------------------------------------------------// //This handler method is executed when the Canvas and// its children have been fully created. private function completeHandler(event:mx.events.FlexEvent):void{ //Set the width and height of the canvas based on// the dimensions of imageB. this.width = 1.3*imageB.width;this.height = 1.3*imageB.height;//Register event listeners to support drag and drop // operations on all three images with the canvas// as the drag target. imageA.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler); imageB.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler); imageC.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);this.addEventListener(DragEvent.DRAG_DROP, dropHandler);this.addEventListener(DragEvent.DRAG_ENTER, enterHandler);} //end completeHandler //--------------------------------------------------//// This event handler initiates the drag-and-drop \// operation for the image that dispatches the // mouseDown event.private function mouseDownHandler( event:MouseEvent):void{//Save the location of the mouse within the image // being dragged. This information will be used// later to properly position the dropped image in// the drop target. this.localX = event.localX;this.localY = event.localY; //Get the drag initiator component from the event// object and cast it to the correct type. var dragInitiator:Image = Image(event.currentTarget); //Add the image being dragged to a DragSource// object and define an identifier as a string. var dragSource:DragSource = new DragSource();dragSource.addData(dragInitiator,"imageObject");//Call the static doDrag method on the DragManager // class to manage the overall drag and drop// operation. DragManager.doDrag(dragInitiator,dragSource,event);}//end mouseDownHandler //--------------------------------------------------////This dragEnter event handler causes the canvas to// be a suitable drop target. private function enterHandler(event:DragEvent):void{if (event.dragSource.hasFormat("imageObject")){ DragManager.acceptDragDrop(Canvas(event.currentTarget)); } //end if} //end enterHandler //--------------------------------------------------////Execute the dragDrop event handler to drop the image// in its new location. Compensate for the fact that // the mouse pointer is not at the upper-left corner// of the image. Also don't allow the image to be // dragged off the left side of the canvas or off the// top of the canvas. private function dropHandler(event:DragEvent):void{//Compute the position of the upper-left corner of// the dropped image. var cornerX:Number = (Canvas(event.currentTarget).mouseX) - localX; var cornerY:Number = (Canvas(event.currentTarget).mouseY) - localY; 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

Miscellaneous

This section contains a variety of miscellaneous materials.

Housekeeping material
  • Module name: Drag and Drop Basics
  • Files:
    • ActionScript0140\ActionScript0140.htm
    • ActionScript0140\Connexions\ActionScriptXhtml0140.htm
PDF disclaimer: Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

-end-

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