Page 1 of 1

Broadcasting events in Flash [Flash 8] Rate Topic: -----

#1 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1641
  • View blog
  • Posts: 8,506
  • Joined: 07-September 06

Post icon  Posted 25 June 2008 - 12:00 AM

Everyone has time when they wish they can get multiple things going at the same time, or close enough to the same time, without having to hard code every portion of everything that is supposed to happen into a single function. This wastes time, and in time will bloat the size of you flash files. Luckily, Flash has added a way to create event listeners and broadcasters so everything goes with a single call (granted it uses a loop to get everything going).

In this tutorial we will be looking at how event listeners adn broadcasters can be made to work together.

First thing first, there is the code. Completed, the code wee will be creating looks like so:

broadcaster = {}; 

listener = {}; 

listener.onEvt = function(){ 

Now the problem is that this code, like much of the code in the world, is useless without a little explanation of what it does.

The lines which do this: broadcaster = {}; and listener = {}; are creating 2 variables (broadcaster and listener) and setting them as objects.

AsBroadcaster.initialize(broadcaster); Makes it so that things can start listening for events being called from the broadcaster object.

REALIZE - The events that can be broadcast don't need to be real events. They can be made up to suit your purposes!

The next line broadcaster.addListener(listener); is used to tell the listener object to be listening for events called by the broadcaster object.

Next is the code creating a function:

listener.onEvt = function(){ 

That tells listener to be listening for the event called onEvt and then do the function it has called onEvt. If the listener object doesn't have a function for the event it is simply ignored. When the event is called the listener object traces (outputs in a separate box, available through Flash) the text "Event!".

The last 2 lines are exactly the same: broadcaster.broadcastMessage("onEvt"); which is basically telling the broadcaster to loop through all its listeners and send a call to the function onEvt.

If you want to try out the code feel free to. It requires no movieclips or anything special to run, just flash and AS 2.0 functionality (will probably work for AS 3.0 also).

In the next tutorial we will look at creating our own, easy/easier to use listener and broadcaster events.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1