-
Why should I use Events?
Why should I use events?
I mean, in order to use events, I have to 1. setup an event dispatcher, 2.add an event listener to an object and 3. have a listener function set up.
Could I not achieve the same thing by just calling a function in whichever object I need to?
What is the benefit? Maybe I'm missing the point....
Anyone care to enlighten me? Cheers!
-
Senior Member
In order to handle any events (functions) you need to create eventListeners for the object, which triggers the event. There is no other way.
- The right of the People to create Flash movies shall not be infringed. -
-
Perhaps...
I didn't explain what I meant clearly enough, so hopefully this will...
Imagine I'm making a game....a "game over" event can take place.
This happens when a playerObject has a y property above 100.
imagine something like this:
Code:
public class Game {
private var __playerObject:Player = new Player();
public function Game () {
__playerObject.y = 102;
updateState();
}
private function updateState() {
if (__playerObject.y>100){
gameOver();
}
}
private function gameOver(){
__playerObject.gameOver();
}
}
In this case, I was able to call a gameOver() method on a __playerObject without any event.
So, what exactly is the case for using an event?
-
Low coupling or writing a class that doesn't know ahead of time what/how many objects will be listening to its events.
The greatest pleasure in life is doing what people say you cannot do.
- Walter Bagehot
The height of cleverness is to be able to conceal it.
- Francois de La Rochefoucauld
-
The primary advantage in situations such as you describe is when the object which would be doing the dispatch doesn't have a reference to everything that would like to know about it.
It's a more object oriented approach which allows levels of structural abstraction.
Let's say your Player object could tell when the game was over and wanted to notify the Game. Let's also say you have an arbitrary number of Players.
Code:
public class Game {
private var _players:Array = new Array();
private var gameTimer:Timer;
public function Game() {
var p:Player;
for (var i:int = 0; i < 10; i++){
p = new Player();
_players.push(p);
p.addEventListener(GameEvent.GAME_OVER, gameOver);
this.addEventListener(GameEvent.TICK, p.update);
}
gameTimer = new Timer(100);
gameTimer.addEventListener(TimerEvent.TIMER, updateState);
gameTimer.start();
}
public function updateState(event:TimerEvent):void{
//any logic such as updating leader board, etc.
dispatchEvent(new GameEvent(GameEvent.TICK));
}
public function gameOver(gevent:GameEvent):void{
gameTimer.stop();
//other cleanup code.
}
If you had kept the architecture you had above, you'd have to test each player for the gameOver condition in your updateState method. Honestly, for a simple game, that's not so bad. But it gets hairy quickly if you don't let objects take care of themselves.
-
Thanks that helps...I can see now how it helps when adding and removing objects from listening, without having to write new code for handling the adding and removing to a list.....
however, the coupling...I don't understand how it is less strongly coupled than just calling a method on an object.
Thanks for the help!
-
more questions...
Originally Posted by 5TonsOfFlax
The primary advantage in situations such as you describe is when the object which would be doing the dispatch doesn't have a reference to everything that would like to know about it.
What is a typical case of this? I can imagine something like this:
Code:
class someClass {
public function someClass(){
var fooInstance:Foo = new Foo();
var barInstance:Bar = new Bar();
//somehow here i'd like to make barInstance listen to an event generated by fooInstance
}
}
how is it possible that I can make barInstance listen to an event generated by fooInstance?
Sorry if these questions are basic. I'm not familiar with events, and it shows
P.S. thanks for all the help, it is slowly getting through to me
-
Yes, that is a good example.
To set up the listener relationship, you'd just need to do this:
Code:
fooInstance.addEventListener(FooEvent.FOO, barInstance.handleFoo);
I like to have the Bar class provide a method which accepts a Foo, but only when you already know that the dispatching instance will be a Foo.
Code:
public class Bar {
public function addFoo(foo:Foo):void{
foo.addEventListener(FooEvent.FOO, handleFoo);
}
//...
}
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|