This page is optimized for mobile devices, if you would prefer the desktop version just click here

0.14 Dragging objects between containers  (Page 5/9)

Listing 5 begins by getting and saving the coordinates of the mouse pointer, relative to the upper-leftcorner of the object that dispatches the mouseDown event. As you saw in the earlier lesson titled Drag and Drop Basics , this information will be used later to properly position the dropped image in the Canvas object.

Get the name of the class from which thedrag initiator was instantiated

The code in Listing 6 is completely new to this lesson.

Get the name of the class from which the drag initiator was instantiated.

var className:String = getQualifiedClassName( event.target);

The Adobe documentation refers to the object that dispatches the mouseDown event to start the drag and drop process as the drag initiator .

Call the standalone getQualifiedClassName function

Listing 6 calls the function named getQualifiedClassName to get a string containing the name of the class from which the drag initiatorobject was instantiated. Note that this is a stand-alone function in the flash.utils package.

Non-unique strings

Different classes return different strings but the strings are not unique. In other words, two or more classes may return the same string. For example,several different Flex components return the same string as the string returned by the Button component.

The three draggable component types used in this program return the following strings :

  • Image returns "mx.controls::Image"
  • Button returns "mx.controls::Button"
  • TextArea returns "mx.core::UITextField"

Save the string

The string returned by the object that dispatched the mouseDown event is saved in the variable named className in Listing 6. This string value will be used later to identify the type of component that dispatched the mouseDown event.

Some of the steps in the process...

When the object being dragged enters one of the Canvas objects, the Canvas object dispatches a dragEnter event, which is a subtype of the class DragEvent . The dragEnter event handler receives an object of type DragEvent , which encapsulates an object of the class DragSource .

The DragSource object

The DragSource object encapsulates a reference to the drag initiator object (the object that dispatched the mouseDown event) . It also encapsulates a format string that can be used to identify the draginitiator object. In this program, that string is used by a particular Canvas object to determine if it will accept a drop by the drag initiator object.

Populate a new DragSource object with the drag initiator and a format string

Listing 7 instantiates a new DragSource object and populates it with the drag initiator and a format string that identifies the type of the drag initiator.The format string is based on the class from which the drag initiator was instantiated using the information in the className variable from Listing 6 in the conditional clause.

Populate a new dragsource object with the drag initiator and a format string.

var dragSource:DragSource = new DragSource(); if(className == "mx.controls::Image"){dragSource.addData(UIComponent( event.currentTarget),"imageObj");}else if(className == "mx.controls::Button"){ dragSource.addData(UIComponent(event.currentTarget),"buttonObj"); }else if(className == "mx.core::UITextField"){dragSource.addData(UIComponent( event.currentTarget),"textAreaObj");} //end else if
<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.