This page require Adobe Flash 9.0 (or higher) plug in.

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

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);
		}
	}
}

Other Cool Related Stuff

3 Comments so far »

  1. Mr.GoodStuff said

    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.

  2. Egypten said

    am May 27 2010 @ 5:45 am

    Very neat! I’m not very used to flash but I plan to learn so I shall bookmark this page.

    Though, much that once required flash can now be done with jQuery.

  3. Michael Jensen said

    am August 14 2010 @ 9:23 am

    Very very nice…

    Normally flash have never been me but today I just set up my first Flash carousel. Very fun and I must say it has open my eyes for the opportunities in that program

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment:

CommentLuv Enabled