Home » Aportes »AS3 »Avanzado »Class » Currently Reading:

onReleaseOutside, onDragOut, onDragOver en ActionScript 3

junio 1, 2007 Aportes, AS3, Avanzado, Class 1 Comment
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:

  1. /* code by: André Michelle - http://blog.andre-michelle.com */
  2.  
  3. package de.popforge.events
  4. {
  5.     import flash.events.Event;
  6.  
  7.     public class SimpleMouseEvent extends Event
  8.     {
  9.         static public  const PRESS:String='onPress';
  10.         static public  const RELEASE:String='onRelease';
  11.         static public  const RELEASE_OUTSIDE:String='onReleaseOutside';
  12.         static public  const ROLL_OVER:String='onRollOver';
  13.         static public  const ROLL_OUT:String='onRollOut';
  14.         static public  const DRAG_OVER:String='onDragOver';
  15.         static public  const DRAG_OUT:String='onDragOut';
  16.  
  17.         public function SimpleMouseEvent(type:String,bubbles:Boolean=true,cancelable:Boolean=false)
  18.         {
  19.             super(type,bubbles,cancelable);
  20.         }
  21.     }
  22. }
  1. /* code by: André Michelle - http://blog.andre-michelle.com */
  2.  
  3. package de.popforge.events
  4. {
  5.     import flash.display.InteractiveObject;
  6.     import flash.display.MovieClip;
  7.     import flash.display.Sprite;
  8.     import flash.display.Stage;
  9.     import flash.events.MouseEvent;
  10.     import flash.utils.Dictionary;
  11.    
  12.     public class SimpleMouseEventHandler
  13.     {
  14.         static public function register( target: InteractiveObject, trackAsMenu: Boolean = false ): void
  15.         {
  16.             new SimpleMouseEventHandler( target, trackAsMenu );
  17.         }
  18.        
  19.         static public function unregister( target: InteractiveObject ): void
  20.         {
  21.             SimpleMouseEventHandler( table[ target ] ).dispose();
  22.            
  23.             delete table[ target ];
  24.         }
  25.        
  26.         static private const table: Dictionary = new Dictionary( true );
  27.        
  28.         //-- target must have set buttonMode = true
  29.         private var target: InteractiveObject;
  30.        
  31.         private var trackAsMenu: Boolean;
  32.        
  33.         public function SimpleMouseEventHandler( target: InteractiveObject, trackAsMenu: Boolean )
  34.         {
  35.             this.target = target;
  36.             this.trackAsMenu = trackAsMenu;
  37.            
  38.             init();
  39.         }
  40.        
  41.         private function init(): void
  42.         {
  43.             if( target is Sprite )
  44.                 Sprite( target ).buttonMode = true;
  45.  
  46.             else if( target is MovieClip )
  47.                 MovieClip( target ).buttonMode = true;
  48.            
  49.             target.addEventListener( MouseEvent.MOUSE_OVER, onTargetMouseOver );
  50.             target.addEventListener( MouseEvent.MOUSE_OUT, onTargetMouseOut );
  51.             target.addEventListener( MouseEvent.MOUSE_DOWN, onTargetMouseDown );
  52.             target.addEventListener( MouseEvent.MOUSE_UP, onTargetMouseUp );
  53.            
  54.             table[ target ] = this;
  55.         }
  56.        
  57.         private function dispose(): void
  58.         {
  59.             target.removeEventListener( MouseEvent.MOUSE_OVER, onTargetMouseOver );
  60.             target.removeEventListener( MouseEvent.MOUSE_OUT, onTargetMouseOut );
  61.             target.removeEventListener( MouseEvent.MOUSE_DOWN, onTargetMouseDown );
  62.             target.removeEventListener( MouseEvent.MOUSE_UP, onTargetMouseUp );
  63.         }
  64.        
  65.         private function onTargetMouseOver( event: MouseEvent ): void
  66.         {
  67.             if( event.buttonDown )
  68.             {
  69.                 if( target.stage.focus == target || trackAsMenu )
  70.                     dispatch( SimpleMouseEvent.DRAG_OVER );
  71.             }
  72.             else
  73.             {
  74.                 dispatch( SimpleMouseEvent.ROLL_OVER );
  75.             }
  76.         }
  77.        
  78.         private function onTargetMouseOut( event: MouseEvent ): void
  79.         {
  80.             if( event.buttonDown )
  81.             {
  82.                 if( target.stage != null )
  83.                 {
  84.                     if( target.stage.focus == target || trackAsMenu )
  85.                         dispatch( SimpleMouseEvent.DRAG_OUT );
  86.                 }
  87.             }
  88.             else
  89.             {
  90.                 dispatch( SimpleMouseEvent.ROLL_OUT );
  91.             }
  92.         }
  93.        
  94.         private function onTargetMouseDown( event: MouseEvent ): void
  95.         {
  96.             dispatch( SimpleMouseEvent.PRESS );
  97.            
  98.             target.stage.addEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
  99.         }
  100.        
  101.         private function onTargetMouseUp( event: MouseEvent ): void
  102.         {
  103.             if( target.stage.focus == target )
  104.                 dispatch( SimpleMouseEvent.RELEASE );
  105.                
  106.             target.stage.removeEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
  107.         }
  108.        
  109.         private function onStageMouseUp( event: MouseEvent ): void
  110.         {
  111.             dispatch( SimpleMouseEvent.RELEASE_OUTSIDE );
  112.            
  113.             Stage( event.currentTarget ).removeEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
  114.         }
  115.        
  116.         private function dispatch( type: String ): void
  117.         {
  118.             target.dispatchEvent( new SimpleMouseEvent( type ) );
  119.         }
  120.     }
  121. }

Podéis ver el post original en el blog de André Michelle, y descargaros los archivos del ejemplo aqui.

Compártelo:

onReleaseOutside, onDragOut, onDragOver en ActionScript 3
Visto 10.626 veces

Currently there is "1 comment" on this Article:

  1. Rafeo dice:

    Ssssss! Este compa sí que le mueve al AS3! Thanks Zguillez.

Comment on this Article:








Twitter: zguillez

AdvertisementAdvertisementAdvertisementAdvertisement

Recibe las novedades por email



Map

Ranking

Codigoactionscript.org: 4.65 sobre 5 (106 valoraciones)

twitter-widget.com