Actionscript Course

Besides the predefined events in ActionScript 3 (MouseEvent, KeyboardEvent, etc.) you can also create other new events (Custom Events), by extending the EventDispatcher class.
- "EventDispatcher" is the base class for events.


To learn how to create a new event, study the following example.
We create a class (named Game) as an extension of the "EventDispatcher" class (see the explanations in code).

Game Class

// Game Class (with a "Custom Event")
package {
  // Imports the classes whose methods are used
  import flash.events.*;           // To be able to use /extend the EventDispatcher Class
  import flash.utils.*;            // Necessary for setTimeout

 // Create the Game class which extends the EventDispatcher
 public class Game extends EventDispatcher
 {
  // Define a constant that will represent the name of the new event
  // Its value gives the name of the event
  public static const GAME_OVER:String = 'gameOver';

  // Private property, used only in the class to store a counting
  private var sec:int = 0;

  // Constructor
  public function Game(end:int)
  {
    // Call the "count()" function with the property "sec" as argument
    count(this.sec);

    // The function "count()" inside the constructor
    function count(sec:int):void
    {
      // If the property 'sec' has the value of 3, calls the "endGame()" method
      if(this.sec==end) endGame();
      else
      {
        trace(sec);                  // Displays in Output the value of the 'sec' parameter
        this.sec = sec+1;            // Modify the value of the "sec" property
        // "this.sec" is the property, 'sec' is the function's parameter

        // Calls the function "count()" every second, with the property "sec" as argument
        setTimeout(count, 1000, this.sec);
      }
    }
  }

  // The private method, called when "sec" reaches the value of the "end" parameter
  // "end" is the constructor's parameter and must be passed when the instance of the classe is created
  private function endGame():void
  {
    // Trigger the event created and represented by this class
    dispatchEvent(new Event(Game.GAME_OVER));
  }
 }
}
- The Game class contains a constant "GAME_OVER" that will represent the event's name, a private property "sec" (used only in the class body) and a private method "endGame()" that will trigger the event created by this class.
- The constructor increments the value of the "sec" property every second, and displays the number in Output panel. When this number becomes equal with the value passed as argument when the instance was created, calls the method "endGame()", that will trigger the event "Game.GAME_OVER".
- This class must be added in a "ActionScript 3.0 Class" document, then saved with the name "Game.as" in the same folder as the FLA document that will use it.

In the Flash document add the following ActionScript code:
// Create an instance of the Game class
var game:Game = new Game(3);

// Register an event listener to detect the Game.GAME_OVER event
game.addEventListener(Game.GAME_OVER, gameOverListener);

// You can also use the value of the GAME_OVER constant ('gameOver', gameOverListener)

// Function called when the event is triggered
function gameOverListener (evt:Event):void
{
  trace('The game is over!');
}
- The event created by the Game class ( Game.GAME_OVER ) is registered as any other type of event, with the "addEventListener()" method applied to this class instance. "Game" represents the event, and "GAME_OVER" is its name.
- When the "sec" property of the Game class reaches the value of 3 ( or any other number added to 'new Game(nr)'), it calls the "endGame()" method, that triggers the event. This action is detected by the event listener registered with "addEventListener()", and will call the "gameOverListener()" function.
- In Output will be displaied:   0 1 2 The game is over!.

Usually the class that represents the event is used in conjunction with another class that access it when declaring the event.


To download the FLA file and "Game.as" with the example from this lesson, click: Creating new events

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Creating new events

Last accessed pages

  1. Get Mime Type of file or string content in PHP (6230)
  2. Countdown Timer with starting time added into a form (11533)
  3. Disable button and Enable it after specified time (17533)
  4. JpGraph - Create Graph, Charts, Plots in PHP (3933)
  5. Simple Admin Login PHP Script (10999)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)