<< Chapter < Page Chapter >> Page >


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>

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


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.


Questions & Answers

where we get a research paper on Nano chemistry....?
Maira Reply
what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
Nasa has use it in the 60's, copper as water purification in the moon travel.
nanocopper obvius
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
analytical skills graphene is prepared to kill any type viruses .
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?