onReleaseOutside, onDragOut, onDragOver en ActionScript 3
Un problema de AS3 es que no trae los eventos de Mouse onReleaseOutside, onDragOut, onDragOver. Visitando la web de André Michelle he visto que ha creado una clase para controlar estos eventos 😉
Estos serían los códigos:
- /* code by: André Michelle - http://blog.andre-michelle.com */
- package de.popforge.events
- {
- import flash.events.Event;
- public class SimpleMouseEvent extends Event
- {
- static public const PRESS:String='onPress';
- static public const RELEASE:String='onRelease';
- static public const RELEASE_OUTSIDE:String='onReleaseOutside';
- static public const ROLL_OVER:String='onRollOver';
- static public const ROLL_OUT:String='onRollOut';
- static public const DRAG_OVER:String='onDragOver';
- static public const DRAG_OUT:String='onDragOut';
- public function SimpleMouseEvent(type:String,bubbles:Boolean=true,cancelable:Boolean=false)
- {
- super(type,bubbles,cancelable);
- }
- }
- }
- /* code by: André Michelle - http://blog.andre-michelle.com */
- package de.popforge.events
- {
- import flash.display.InteractiveObject;
- import flash.display.MovieClip;
- import flash.display.Sprite;
- import flash.display.Stage;
- import flash.events.MouseEvent;
- import flash.utils.Dictionary;
- public class SimpleMouseEventHandler
- {
- static public function register( target: InteractiveObject, trackAsMenu: Boolean = false ): void
- {
- new SimpleMouseEventHandler( target, trackAsMenu );
- }
- static public function unregister( target: InteractiveObject ): void
- {
- SimpleMouseEventHandler( table[ target ] ).dispose();
- delete table[ target ];
- }
- static private const table: Dictionary = new Dictionary( true );
- //-- target must have set buttonMode = true
- private var target: InteractiveObject;
- private var trackAsMenu: Boolean;
- public function SimpleMouseEventHandler( target: InteractiveObject, trackAsMenu: Boolean )
- {
- this.target = target;
- this.trackAsMenu = trackAsMenu;
- init();
- }
- private function init(): void
- {
- if( target is Sprite )
- Sprite( target ).buttonMode = true;
- else if( target is MovieClip )
- MovieClip( target ).buttonMode = true;
- target.addEventListener( MouseEvent.MOUSE_OVER, onTargetMouseOver );
- target.addEventListener( MouseEvent.MOUSE_OUT, onTargetMouseOut );
- target.addEventListener( MouseEvent.MOUSE_DOWN, onTargetMouseDown );
- target.addEventListener( MouseEvent.MOUSE_UP, onTargetMouseUp );
- table[ target ] = this;
- }
- private function dispose(): void
- {
- target.removeEventListener( MouseEvent.MOUSE_OVER, onTargetMouseOver );
- target.removeEventListener( MouseEvent.MOUSE_OUT, onTargetMouseOut );
- target.removeEventListener( MouseEvent.MOUSE_DOWN, onTargetMouseDown );
- target.removeEventListener( MouseEvent.MOUSE_UP, onTargetMouseUp );
- }
- private function onTargetMouseOver( event: MouseEvent ): void
- {
- if( event.buttonDown )
- {
- if( target.stage.focus == target || trackAsMenu )
- dispatch( SimpleMouseEvent.DRAG_OVER );
- }
- else
- {
- dispatch( SimpleMouseEvent.ROLL_OVER );
- }
- }
- private function onTargetMouseOut( event: MouseEvent ): void
- {
- if( event.buttonDown )
- {
- if( target.stage != null )
- {
- if( target.stage.focus == target || trackAsMenu )
- dispatch( SimpleMouseEvent.DRAG_OUT );
- }
- }
- else
- {
- dispatch( SimpleMouseEvent.ROLL_OUT );
- }
- }
- private function onTargetMouseDown( event: MouseEvent ): void
- {
- dispatch( SimpleMouseEvent.PRESS );
- target.stage.addEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
- }
- private function onTargetMouseUp( event: MouseEvent ): void
- {
- if( target.stage.focus == target )
- dispatch( SimpleMouseEvent.RELEASE );
- target.stage.removeEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
- }
- private function onStageMouseUp( event: MouseEvent ): void
- {
- dispatch( SimpleMouseEvent.RELEASE_OUTSIDE );
- Stage( event.currentTarget ).removeEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
- }
- private function dispatch( type: String ): void
- {
- target.dispatchEvent( new SimpleMouseEvent( type ) );
- }
- }
- }
Podéis ver el post original en el blog de André Michelle, y descargaros los archivos del ejemplo aqui.
Compártelo:
Visto 10.703 veces
Ssssss! Este compa sà que le mueve al AS3! Thanks Zguillez.