Hi,
It's good to extend the Event Class because you can define your own
event constants and also pass data around inside your event...
The reason you do this is because you cannot rely on things in Flash
don't happen in the same sequence all the time, so it's like asking
someone to make something for you then tell you when they are finished
so you can take the thing they have made and then do your bit.
I guess an analogy would be like buying a prefab house from
http://www.huf-haus.com, which is made in a factory and assembled in a
very predictable way - almost "synchronous". Compare this to the
majority of house builders who might turn up and do a bit of work now
and then, but you can't rely on them finishing on an exact date, (the
one you book the plasterer for). This is pretty much "asynchronous".
You just have to wait for the builders to tell you they have finished,
then ring the plasterer, etc.
Web design is pretty much the same when some customers are involved
- you can schedule all you want, but it goes out the window as soon as
you ask the client to provide some content. :)
Here is a quick example of an extended event that has constants and
also defines a data variable of any type so I can pass information to
listeners, below is an example of using it in context. It's not perfect
- I am being a bit lazy by not typing my data and could probably improve
stuff, but it's a start:
//Simple class to extend event and pass score data around from AMF
method call results
package events
{
import flash.events.Event;
public class ScoreBoardEvent extends Event
{
public var data:*;
public static const SCORE_SERVER_ERR:String = "Problem with the
server";
public static const SCORE_SUBMIT_OK:String = "Score submitted ok";
public static const SCORE_SUBMIT_ERR:String = "Score submit error";
public static const SCORE_LIST_OK:String = "Scores listed ok";
public static const SCORE_LIST_ERR:String = "Scores listing error";
public function ScoreBoardEvent(type:String, data:*) :void {
super(type);
this.data = data;
}
//You need to override the clone function to ensure the correct
type gets passed around by the event mechanism.
override public function clone():Event {
return new ScoreBoardEvent(this.type, this.data);
}
}
}
/*My ScoreBoard class talks to the server and calls methods via
remoting, this is the method that handles the result of one remote
method call
ScoreBoard extends Sprite because I needed it on the stage, you can
always implement the EventDispatcher interface for objects that do not
extend something that does :
This is a snippet.
*/
private function submitResultHandler(result:*):void
{
try {
if (result.count) {
FlashConnect.trace("submitResultHandler " +
result.count);
dispatchEvent(new
ScoreBoardEvent(ScoreBoardEvent.SCORE_SUBMIT_OK, result));return;
return;
}
}
catch (err:Error) {
//do nothing - we just get an exception if count does
not exist...
}
FlashConnect.trace("submitResultHandler " + result.error +
", " + result.faultString);
dispatchEvent(new
ScoreBoardEvent(ScoreBoardEvent.SCORE_SUBMIT_ERR, result.faultString));
}
//Then in another class that uses ScoreBoard - ScoresScreen I listen for
events from the ScoreBoard
import events.ScoreBoardEvent;
public class ScoresScreen extends Sprite {
public function ScoresScreen() {
var sc:ScoreBoard = ScoreBoard.getInstance();
//If the thing you want to listen to dispatches events, this
is easy.
sc.addEventListener(ScoreBoardEvent.SCORE_LIST_OK, makeScores);
sc.addEventListener(ScoreBoardEvent.SCORE_LIST_ERR, showError);
}
private function makeScores(evt:ScoreBoardEvent):void {
//...
_currentOffset = evt.data.offset;
_totalScores = evt.data.count;
var rank:int = _currentOffset + 1;
_rankText.htmlText = _namesText.htmlText =
_scoresText.htmlText = "";
var list:Object = evt.data.list;
for (var i:String in list) {
_rankText.htmlText += rank + "\n";
_namesText.htmlText += String(list[i][0]).toUpperCase()
+ "\n";
_scoresText.htmlText += list[i][1] + "\n";
trace(i + " = " + list[i][0] + ", " + list[i][1]);
rank++;
}
}
private function showError(evt:ScoreBoardEvent):void {
trace("error " + evt.data);
//More stuff here...
}
}
Dwayne Neckles wrote:
Why is it good to extend the Event Class in your application?
I understand that you can create your own events with it that are specific
to your app but does anyone have