Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-24-2008, 06:39 PM   #1
rrh
Senior Member
 
Join Date: Dec 2006
Posts: 967
Default 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:
ActionScript Code:
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:
ActionScript Code:
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.
rrh is offline   Reply With Quote
Old 01-25-2008, 09:30 AM   #2
jsebrech
Joeri Sebrechts
 
Join Date: Apr 2005
Location: Antwerp, Belgium
Posts: 1,462
Default

Since the event name is just a string, you can start it with something unique to yourself or your project: "com.somedomain.someapp.someevent".
jsebrech is offline   Reply With Quote
Old 01-27-2008, 12:23 PM   #3
creynders
flash veteran
 
creynders's Avatar
 
Join Date: May 2005
Location: Belgium
Posts: 900
Default

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
creynders is offline   Reply With Quote
Old 01-30-2008, 05:28 PM   #4
rrh
Senior Member
 
Join Date: Dec 2006
Posts: 967
Default

Okay, so I would have stuff like:
ActionScript Code:
var loadingEvent:LoadingEvent = new LoadingEvent(LoadingEvent.COMPLETE,true);     dispatchEvent(loadingEvent);
ActionScript Code:
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:
ActionScript Code:
addEventListener(LoadingEvent.COMPLETE,loadedFunction); addEventListener(PlayingEvent.COMPLETE,playedFunction);
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.

Last edited by rrh; 01-30-2008 at 05:30 PM..
rrh is offline   Reply With Quote
Old 01-31-2008, 11:31 AM   #5
creynders
flash veteran
 
creynders's Avatar
 
Join Date: May 2005
Location: Belgium
Posts: 900
Default

Quote:
Originally Posted by rrh View Post
Okay, so I would have stuff like:
ActionScript Code:
var loadingEvent:LoadingEvent = new LoadingEvent(LoadingEvent.COMPLETE,true);     dispatchEvent(loadingEvent);
ActionScript Code:
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:
ActionScript Code:
addEventListener(LoadingEvent.COMPLETE,loadedFunction); addEventListener(PlayingEvent.COMPLETE,playedFunction);
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
creynders is offline   Reply With Quote
Old 02-07-2008, 02:25 PM   #6
panel
AS3
 
panel's Avatar
 
Join Date: Mar 2007
Location: Warsaw
Posts: 1,761
Send a message via Skype™ to panel
Default

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.
panel is offline   Reply With Quote
Old 02-07-2008, 02:40 PM   #7
the binary
Senior Member
 
the binary's Avatar
 
Join Date: May 2003
Location: berlin
Posts: 361
Default

Quote:
so I don't see how it would distinguish between two with the same ID string but different classes.
what about 'instanceof'

//b.
the binary is offline   Reply With Quote
Old 02-07-2008, 02:58 PM   #8
panel
AS3
 
panel's Avatar
 
Join Date: Mar 2007
Location: Warsaw
Posts: 1,761
Send a message via Skype™ to panel
Default

Type checking
ActionScript Code:
var square:Sprite = new Sprite(); trace(square instanceof Sprite); //or trace(square is Sprite)
panel is offline   Reply With Quote
Old 03-06-2008, 06:55 AM   #9
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Quote:
Originally Posted by rrh View Post
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.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
KEY_UP events missing aerosoul ActionScript 3.0 5 05-29-2008 07:39 AM
KB Events / Focus upon re-entry to html window Spire ActionScript 3.0 0 12-30-2007 11:28 PM
Timer and Events not reliable? jsimpson ActionScript 3.0 5 06-06-2007 10:00 PM
Assigning Multiple Events to One Function timothy ActionScript 2.0 1 04-11-2006 10:17 PM
Missing events for components? Denaes Components 0 10-05-2003 05:04 PM


All times are GMT. The time now is 06:58 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.