View Full Version : Naming events
What I was wondering about, was when naming one of my own events, how do I best ensure the name of my event never conflicts with an existing name?
For example:
var gameWinEvent:Event = new Event("wonGame",true);
dispatchEvent(gameWinEvent);
(Now I know it's more up to standards if I make a class that extends Event and make "wonGame" a static constant called GameEvents.WONGAME but ignore that for now.)
How can I be certain there is not already an event named "wonGame?"
To see if there was a convention, I ran this:
trace(Event.ACTIVATE + " " + MouseEvent.CLICK + " " + ProgressEvent.PROGRESS); And got this result:
activate click progress
Which doesn't indicate any way to guarantee an event name is unique other than going through Event and all its descendants for every single static constant to see if it's been used.
jsebrech
01-25-2008, 09:30 AM
Since the event name is just a string, you can start it with something unique to yourself or your project: "com.somedomain.someapp.someevent".
creynders
01-27-2008, 12:23 PM
Why would you need a unique event String? They do not conflict. You can have for example a LoadingEvent.COMPLETE and a PlayingEvent.COMPLETE
In reality both strings will be equal ("complete") but it doesn't matter since in one case you'll be listening for a LoadingEVent and in the other for a PlayingEvent
Okay, so I would have stuff like:
var loadingEvent:LoadingEvent = new LoadingEvent(LoadingEvent.COMPLETE,true);
dispatchEvent(loadingEvent);
var playingEvent:PlayingEvent = new PlayingEvent(PlayingEvent.COMPLETE,true);
dispatchEvent(playingEvent);
So the event being dispatched is clearly of one subclass of Event or another. But then where I add event listeners watching for these events:
addEventListener(LoadingEvent.COMPLETE,loadedFunct ion);
addEventListener(PlayingEvent.COMPLETE,playedFunct ion);
The addEventListener method is being passed an ID string and a function. There's no info really on the event other than the ID string, so I don't see how it would distinguish between two with the same ID string but different classes.
creynders
01-31-2008, 11:31 AM
Okay, so I would have stuff like:
var loadingEvent:LoadingEvent = new LoadingEvent(LoadingEvent.COMPLETE,true);
dispatchEvent(loadingEvent);
var playingEvent:PlayingEvent = new PlayingEvent(PlayingEvent.COMPLETE,true);
dispatchEvent(playingEvent);
So the event being dispatched is clearly of one subclass of Event or another. But then where I add event listeners watching for these events:
addEventListener(LoadingEvent.COMPLETE,loadedFunct ion);
addEventListener(PlayingEvent.COMPLETE,playedFunct ion);
The addEventListener method is being passed an ID string and a function. There's no info really on the event other than the ID string, so I don't see how it would distinguish between two with the same ID string but different classes.
Ah, yes, that's true. I guess I've always automatically made some kind of distinction while naming event types, like for instance PLAY_COMPLETE and LOAD_COMPLETE
panel
02-07-2008, 02:25 PM
I believe that key is at dispatch event method.You are distatching event of specified type, so AV2 can check type of dispatched event, and filter only those who match specified type. Now it has list of pairs eventHandlerFunction/eventType (String), so is just matter to check with ones fit, and call proper function.
the binary
02-07-2008, 02:40 PM
so I don't see how it would distinguish between two with the same ID string but different classes.
what about 'instanceof'
//b.
panel
02-07-2008, 02:58 PM
Type checking
var square:Sprite = new Sprite();
trace(square instanceof Sprite);
//or
trace(square is Sprite)
Flash Gordon
03-06-2008, 06:55 AM
What I was wondering about, was when naming one of my own events, how do I best ensure the name of my event never conflicts with an existing name?
Well, the answer is just experience. The prepend your own special character is a nice idea, but back to the experience part. If you have a class that extends movieclip and you want to listen for a certain event dispatched from that class from experience you no not to call you event "CLICK." So perhaps you call you event "PROXY_CLICK" because if you listen for ProxyMouseEvent.CLICK you will by default be listening to MouseEvent.CLICK as well.
So, it's just experience and to be knowledgeable about your surroundings.
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.