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

0.14 Dragging objects between containers  (Page 6/9)

Can the drag initiator object be dropped?

A Canvas object that examines the format string from the DragSource object later in the program will use the format string to determine if it is willing to allow the draginitiator object to be dropped on it.

Cast the current target to type UIComponent

The term event.currentTarget is a reference to the object that dispatched the mouseDown event, which is the drag initiator. However, when the currentTarget is extracted from the MouseEvent object, it is returned as type Object . In order for it to be suitable for use as the drag initiator, it must be cast to type UIComponent .

Relationships among the strings

I could have simply used the strings from the above list as the format strings, but I decided to create new strings to show that they are really independent of one another.

The relationships that I created between the strings in the above list and the format strings is shown below:

  • Image returns "mx.controls::Image" -- "imageObj"
  • Button returns "mx.controls::Button" -- "buttonObj"
  • TextArea returns "mx.core::UITextField" -- "textAreaObj"

Initiate the drag and drop operation

Listing 8 calls the static method named doDrag of the DragManager class to initiate the drag and drop operation.

Initiate the drag and drop operation.

DragManager.doDrag(UIComponent(event.currentTarget), dragSource,event);}//end mouseDownHandler

I explained the use of this method in the previous lesson titled Drag and Drop Basics so I will refer you back to that lesson for a detailed explanation.

Register a different dragEnter event handler on each Canvas object

Returning to the discussion of the creationComplete event handler, Listing 9 registers a different dragEnter event handler on each Canvas object.

Register a different dragenter event handler on each canvas object.

//Register a different dragEnter event handler on // each Canvas object to make it possible for each// Canvas object to accept only two of the three // types of components for dropping.canvasA.addEventListener(DragEvent.DRAG_ENTER, enterHandlerA);canvasB.addEventListener(DragEvent.DRAG_ENTER, enterHandlerB);canvasC.addEventListener(DragEvent.DRAG_ENTER, enterHandlerC);

Dispatching dragEnter events

As you learned in the earlier lesson titled Drag and Drop Basics , when the user moves the drag proxy over another component, that component dispatches a dragEnter event.

If a dragEnter event handler has been registered on that component, the handler method is executed. If the code in the event handler accepts the drag, it becomes the drop target and receives dragOver , dragExit , and dragDrop events.

I will explain the code in the dragEnter event handlers later. Right now, let's look at the remainder of the creationComplete event handler.

Register the same dragDrop event handler on all three Canvas objects

Listing 10 registers the same dragDrop event handler on each of the three Canvas objects

<< 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.