Flash / Flex / ActionScript/Development/Event

Материал из Web эксперт
Перейти к: навигация, поиск

Add Event listener

 
package {
  import flash.display.*;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main() {
      var urlLoader:URLLoader = new URLLoader(  );
      urlLoader.addEventListener(Event.ruPLETE,
                                 completeListenerA,
                                 false,
                                 0);
      urlLoader.addEventListener(Event.ruPLETE,
                                 completeListenerB,
                                 false,
                                 1);
      urlLoader.load(new URLRequest("someFile.txt"));
    }
    private function completeListenerA (e:Event):void {
      trace("Listener A: Load complete");
    }
    private function completeListenerB (e:Event):void {
      trace("Listener B: Load complete");
    }
  }
}



Event Listener Priority

 
package {
  import flash.display.*;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main() {
      var urlLoader:URLLoader = new URLLoader(  );
      // Registration order determines execution order
      urlLoader.addEventListener(Event.ruPLETE, completeListenerA);
      urlLoader.addEventListener(Event.ruPLETE, completeListenerB);
      urlLoader.load(new URLRequest("someFile.txt"));
    }
    private function completeListenerA (e:Event):void {
      trace("Listener A: Load complete");
    }
    private function completeListenerB (e:Event):void {
      trace("Listener B: Load complete");
    }
  }
}



Handling Events: a simple application that draws successive random lines

 
package {
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class Main extends Sprite {
        
        public function Main(  ) {
            graphics.lineStyle(1, 0, 1);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        private function onEnterFrame(event:Event):void {
            graphics.lineTo(Math.random(  ) * 400, Math.random(  ) * 400);
        }
    }
}



Listeners for the Event.RESIZE event must be registered with Flash Player"s Stage instance,

 
package {
  import flash.display.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main(  ) {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.addEventListener(Event.RESIZE, resizeListener);
    }
    private function resizeListener (e:Event):void {
      trace("Flash Player was resized");
    }
  }
}



Preventing Default Event Behavior

 
package {
  import flash.display.*;
  import flash.text.*;
  import flash.events.*;
  public class Main extends Sprite {
    private var t:TextField;
    public function Main() {
      t = new TextField();
      t.border     = true;
      t.background = true;
      t.type = TextFieldType.INPUT
      addChild(t);
      t.addEventListener(TextEvent.TEXT_INPUT, textInputListener);
    }
    private function textInputListener (e:TextEvent):void {
      trace("Attempted text input: " + e.text);
      e.preventDefault(  );
      t.appendText("x");
    }
  }
}



Responding to Event.ACTIVATE and Event.DEACTIVATE

 
package {
  import flash.display.*;
  import flash.utils.*;
  import flash.events.*;
  public class Main extends Sprite {
    private var timer:Timer;
    private var rect:Sprite;
    public function Main() {
      rect = new Sprite(  );
      rect.x = 200;
      rect.y = 200;
      rect.graphics.lineStyle(1);
      rect.graphics.beginFill(0x0000FF);
      rect.graphics.drawRect(0, 0, 150, 75);
      addChild(rect);
      addEventListener(Event.ACTIVATE, activateListener);
      addEventListener(Event.DEACTIVATE, deactivateListener);
      timer = new Timer(50, 0);
      timer.addEventListener(TimerEvent.TIMER, timerListener);
    }
    private function timerListener (e:TimerEvent):void {
      rect.rotation += 10;
    }
    private function activateListener (e:Event):void {
      timer.start(  );
    }
    private function deactivateListener (e:Event):void {
      timer.stop(  );
    }
  }
}



Responding to Event.RESIZE.

 
package {
  import flash.display.*;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main (  ) {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.addEventListener(Event.RESIZE, resizeListener);
    }
    private function resizeListener (e:Event):void {
      trace("The application window changed size!");
      trace("New width:  " + stage.stageWidth);
      trace("New height: " + stage.stageHeight);
    }
  }
}



The Event.MOUSE_LEAVE Event

 
package {
  import flash.display.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main(  ) {
      stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
    }
    private function mouseLeaveListener (e:Event):void {
      trace("The mouse has left the building.");
    }
  }
}