Drag & Drop and Import Library MovieClip in DocumentClass
This post is the extension of the previous one here on how to add Event Listener to control buttons, and MovieClips when there is interaction.
Like previous of the post, we are going to create a flash sample on how to drag and drop on a newly created MovieClip and how to import an existing MovieClip from the library.
Drag and Drop EventListener for MovieClip and Sprite
Given the scenario the Yellow Star is a premade movieclip in library and the red rectangle one is a movieclip generated from script.Download or refer to the source code below.
package{ import flash.display.*; import flash.text.*; //Add flash event listeners import into the scene. import flash.events.* //A Sprite is like MovieClip but lacks the timeline feature. public class AS_DragDrop extends MovieClip { //Declares a Public MovieClip public var myMovieClip:MovieClip; //Declares a Public MovieClip instance of MC_STAR linkage name public var myStarMC:MC_STAR; public function init() { var myMovieClip = new MovieClip(); //You have to draw the movieclip before assigning any coordinates to it. myMovieClip.graphics.beginFill(0xFF0000); myMovieClip.graphics.drawRect(0, 0, 100, 80); myMovieClip.graphics.endFill(); myMovieClip.x = 100; myMovieClip.y = 100; addChild(myMovieClip); //Add a dragdrop eventlistener to it. myMovieClip.addEventListener(MouseEvent.MOUSE_DOWN,start_drag); myMovieClip.addEventListener(MouseEvent.MOUSE_UP,stop_drag); import_library_MC(); } //void is used to memory optimization if the does not return any values //An eventlistener trigger function must always have event as parameters private function start_drag(event:Event):void{ event.target.startDrag(false); } private function stop_drag(event:Event):void{ event.target.stopDrag(); } //This method imports MC_STAR MovieClip to scene(see library) private function import_library_MC():void{ var myLuckyStar = new MC_STAR(); myLuckyStar.x = 200; myLuckyStar.y = 200; addChild(myLuckyStar); } } }


























am December 3 2008 @ 5:47 pm
Extra notes : To those using EventListener, to make your flash portable in a sense of being able to load to other loaderClass(attachMovie) …try not use/depend on stage too much.