AS3 Buttons and Drag & Drop

event_trigger In ActionScript 3.0, buttons and interactive movieclip stuff is scripted differently. To understand in depth about how ActionScript 3.0 works for interactivity see the diagram beside.

Diagram besides show, a pin with a tag attached to it. In Action Script 3.0, all events such as mouse clicks, mouse move, mouse over, keyboard inputs, even movieclip’s frame movement is controlled using event listeners. You can attached this event listeners to any movieClip, components, stage or sprites to add interactivity features to it.

Just like how the pin works…just attach the pin to the object.

Each event listeners has a trigger function. Trigger function is a script which triggers because of the specific event for example, a mouse click. This means if you attached an event listener for mouse click on a button, the trigger function itself will be the script for the button. This is how you make button scripts in Action Script 3.0. Similar to our old on(Press) function.

If you have a button with the instance name of Button1, try attaching an event listener for mouse click, to it like example below.

Button1.addEventListener(MouseEvent.CLICK,ButtonClick);
function ButtonClick(event:Event){
      trace("This button is Clicked!");
}

For drag and drop of a MovieClip, we use two event Listeners. The Mouse_Down and Mouse_Up. For script below you can use MovieClipName.startDrag as trigger function to have a more precise control over it. Sometimes if there’s more than one movieclip inside event.target will specifically target the one the user is clicking – making tat movieclip to be dragged instead of the whole thing

See Example of Flash Below

MovieClipName.addEventListener(MouseEvent.MOUSE_DOWN,DragStart);
MovieClipName.addEventListener(MouseEvent.MOUSE_UP,DragStop);
function DragStart(event:Event){
	trace("Drag Started!");
	event.target.startDrag(false);
}
 
function DragStop(event:Event){
	trace("Drag Stoped!");
	event.target.stopDrag();
}

Scripts are available for download here.

Download Here