nathandemick.com

Add an Event Listener in Actionscript 3

Importing graphics into your Flash program is all well and good. You can create movies or animation, but the real interesting thing is taking user input and having your program react to it. The way to do that in Actionscript is to add "Event Listeners" to objects you create. Whenever an event occurs (such as a keypress, mouse movement, or whatever), your Event Listener checks to see if it's supposed to do anything. If so, then it runs a function with code that you specify. So for example, say the user presses the "left arrow" key. Your keyboard input event handler sees this, and runs appropriate code (in this case, it might move a game character left).

One of the most basic events in Actionscript is the "enter frame" event. This triggers every time the SWF is redrawn, and basically corresponds to the SWF frame rate. In the following example, we're going to create a new class, and add an "enter frame" event listener to it, so that whenever a new instance of that class is created, a function will run at frame rate.

package {
  import flash.display.Sprite;
  import flash.events.Event;  // Required to add event listeners
  
  // Set our SWF framerate, width, height, and background color
  [SWF(frameRate='30', width='640', height='480', backgroundColor='0xffffff')]

  public class EventListenerExample extends Sprite {

    [Embed(source="ball.svg")]
    public var BallGraphic:Class;
	
      // Variables used to store movement data
      private var dx:int = 5;
      private var dy:int = 5;
	
    public function EventListenerExample():void
    {
      // Create instance of imported graphic
      var b:Sprite = new BallGraphic();

      // Add to this display object
      this.addChild(b);

      // Attach event listener
      // Listen for "ENTER_FRAME" event
      // Run the "enterFrame" function 
      this.addEventListener(Event.ENTER_FRAME, enterFrame);
    }

	public function enterFrame(e:Event):void 
	{
	  // Increment position
	  this.x += this.dx;
	  this.y += this.dy;
		
	  // If object passes beyond SWF boundaries,
	  // reverse direction
	  if(this.x >= 640 - this.width || this.x <= 0)
	    this.dx *= -1;
	  if(this.y >= 480 - this.height || this.y <= 0)
	    this.dy *= -1;
	}
  }
}

Save this code in a file named 'EventListenerExample.as'. You can see that we've elaborated a bit on our last example, importing and displaying an SVG graphic. However, this time we add some variables to the class that represent speed (delta-x and delta-y). We attach the event listener by calling this.addEventListener (Event.ENTER_FRAME, enterFrame);. The first argument is the type of event the listener is triggered by, and the second is the name of the function to run when the event is detected.

In the declaration of the 'enterFrame' function, we pass it details of the event that triggered it by putting e:Event in the arguments list. The function doesn't actually use that information, but simply moves our graphic around, bouncing off the "walls" of the SWF. Movement is just one possible application of the 'enter frame' event; collision detection can also be calculated this way, among other things.

· 0 comments


Comments