<< Chapter < Page Chapter >> Page >

Register the same dragdrop event handler on all three canvas objects.

//Register the same dragDrop event handler on all // three Canvas objects.canvasA.addEventListener(DragEvent.DRAG_DROP, dropHandler);canvasB.addEventListener(DragEvent.DRAG_DROP, dropHandler);canvasC.addEventListener(DragEvent.DRAG_DROP, dropHandler);} //end completeHandler

The code in Listing 10 will cause another event handler to be called on a Canvas object after it dispatches a dragEnter event and accepts the drag.

Listing 10 also signals the end of the creationComplete event handler.

The dragEnter event handlers

A different dragEnter event handler was registered on each Canvas object in Listing 9.

Each of the dragEnter event handlers is executed when the dragged component enters the Canvas object on which the handler is registered. The event handlers decide whether or not to accepta drop on the basis of the format string associated with the type of object being dragged. Note thateach Canvas object will accept two of the three types of objects.

Will only explain one of the dragEnter event handlers

Because of the similarity of the three event handlers, I will explain only one of them. You can view the code for all three in Listing 14 near the endof the lesson. Listing 11 shows a dragEnter event handler for the top Canvas object shown in Figure 1.

A dragenter event handler for the top canvas object.

//This dragEnter event handler causes the canvas to // accept images and buttons.private function enterHandlerA(event:DragEvent):void{ if ((event.dragSource.hasFormat("imageObj")) ||(event.dragSource.hasFormat("buttonObj"))){DragManager.acceptDragDrop( Canvas(event.currentTarget));} //end if } //end enterHandler

Confirm the correct format string

The event handler shown in Listing 11 causes the Canvas object to accept onlyimages and buttons. The only difference between the code in Listing 11 and similar code in the earlier lesson titled Drag and Drop Basics is the use of the logical-or (||) operator in Listing 11 to accept either ofthe two string instead of just one.

The code in Listing 11 checks to confirm that the format string in the DragSource object matches either "imageObj" or "buttonObj" (see Listing 7) . If so, it calls the static acceptDragDrop method on the DragManager class, passing a reference to itself as a parameter in the method call.

Accept the dragged object

The call to the acceptDragDrop method notifies the DragManager that the Canvas object is willing to accept the contents of the DragSource object being dropped onto itself.

The dragDrop event handler

The dragDrop event handler was registered on all three Canvas objects in Listing 10. This method is executed after a Canvas object accepts the drag and the user releases the mouse button while the drag proxy isover the Canvas .

The dragDrop event handler method is shown in its entirety in Listing 12.

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