Welcome to Dream.In.Code
Getting Help is Easy!

Join 117,156 Programmers for FREE! Ask your question and get quick answers from experts. There are 2,383 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Broadcasting events in Flash

 
Reply to this topicStart new topic

> Broadcasting events in Flash, [Flash 8]

BetaWar
Group Icon



post 25 Jun, 2008 - 12:00 AM
Post #1


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:

CODE
broadcaster = {};
AsBroadcaster.initialize(broadcaster);

listener = {};
broadcaster.addListener(listener);

listener.onEvt = function(){
  trace("Event!");
}
broadcaster.broadcastMessage("onEvt");
broadcaster.broadcastMessage("onEvt");


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:

CODE
listener.onEvt = function(){
  trace("Event!");
}


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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/6/08 10:40AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month