I am making a game using AS3. I have three classes (Spaceship, UFO, and Scoreboard) which are all children of the class GameEngine.
I want to put eventListeners in Scoreboard and then send dispatchEvents from Spaceship and UFO to update the scoreboard instance.
Since Spaceship and UFO are neither parents or children of Scoreboard, adding a bubbling=true parameter to the dispatchEvent does nothing.
How do I get Scoreboard to listen for *dispatchEvent*s without doing this:
GameEngine.scoreboard.dispatchEvent(new Event("shipWasHit", true));
This seems silly to do it this way. Why would I use a dispatchEvent at all -- why not call the function directly? And what if I want other classes to listen for the same dispatchEvent?
Please advise.
Your Spaceships and UFO's would do this:
and your Scoreboard would need to know which spaceships and UFO's to listen to for those events:
and
A good place to add those listeners is in the class that creates those Spaceships and UFO's, or in the Scoreboard itself if at some point you tell it what ships and UFO's to listen to.