I’m working on my first as3 class. There has been a discussion of it going on over at actionscript.org I’m questioning whether or not this should extend the MovieClip class. In any regard it works well for my needs.
In working on this class, I’ve stumbled across a very useful event “MOUSE_LEAVE” It is a constant of the Event class and not part of the MouseEvent class. It’s also worthwhile to mention that the event listener is added to the stage object once I’ve called the startDragger method, instead of in the constructor. Having the stage object listen to the event whilst instantiating my class simply does not work. I’d be interested in figuring out why this is so.
Without further ado here is the code.
package com.bowievanling{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.DisplayObject;
public class Dragger extends MovieClip{
private var _dragParent:Object;
public function Dragger(){
addEventListener(MouseEvent.MOUSE_DOWN, startDragger);
addEventListener(MouseEvent.MOUSE_UP, mouseStopHandle);
if(stage){
stage.addEventListener(MouseEvent.MOUSE_UP, mouseStopHandle);
}
}
private function startDragger(evt:MouseEvent):void {
//trace("dragParent = "+_dragParent);
_dragParent = evt.target.parent;
_dragParent.startDrag();
stage.addEventListener(Event.MOUSE_LEAVE, eventStopHandle);
}
private function mouseStopHandle(evt:MouseEvent){
stopDragger();
}
private function eventStopHandle(evt:Event){
stopDragger();
}
private function stopDragger(){
//test to make sure that there is a valid reference to _dragparent.
if(_dragParent){
_dragParent.stopDrag();
}
}
}
}
No Comment Received
Leave A Reply