• Keep in touch:
  • Linked In
  • Twitter
  • RSS

What is an event?

A Flash movie would simply be a series of static frames were it not for the ability of the Flash Player to respond to events such as a hey being pressed or a mouse button clicked. Handlers can be registered allowing movie clips and buttons to execute a set of actions in response to different types of events, enabling you to create movies that behave more like applications than animations.

The Flash Player generates an event when any of the following types of action occur:

  • A key is pressed on the keyboard.
  • The mouse enters the screen
  • A mouse button is clicked.
  • The mouse is moved anywhere on the screen.
  • The mouse is moved over a button or movie clip.
  • The mouse is dragged over a button or movie clip.
  • The movie clip is loaded or unloaded.
  • A movie clip or button object is instantiated.
  • Data is loaded from an external source.

Both movie clips and buttons can respond to all the events generated by the Flash Player.

Event handlers for movie clips are created using the Transform class, EventHandler and registered when the movie clip is placed on the display list using Place2. Event handlers for buttons are also created using the EventHandler class. Details of each type of event can be found in the documentation for the Event enum type.

Rather than generate the actions and handler objects directly it is much easier to write the event handler(s) in Actionscript and then use Translate to compile the code to generate the actions and handler objects. For example the following event handlers allow a movie clip to be dragged around the screen when clicked on by the mouse:

    onClipEvent(mouseDown) {
	    startDrag(this);
    }
	
	onClipEvent(mouseUp) {
	    stopDrag();
	}

Event handlers for buttons are defined using the on() Actionscript statement:

	on(mouseDown) {
	    ...
    }
	
	on(mouseUp) {
	    ...
	}

If the handlers are stored in a file then the code can be loaded, compiled and added to the movie clip or button in a few lines of code:

  
    String filename = "handlers.as";
    String script = contentsOfFile(filename);
    
    ASCompiler compiler = new ASCompiler();
    List<Action>handlers = compiler.compile(script);
	
    movie.add(Place2.show(id, layer, x, y).setName("clip"));
    
    for (Action handler : handlers) {
        location.add((EventHandler) handler);
    }

Using Actionscript separates the code defining the behaviour of objects in a movie from the code used to define the movie structure - simplifying the process of creating and maintaining movies.