Re: [Flashcoders] Why is good to extend an Event Class

2008-05-06 Thread Steven Sacks
It's not really important to understand the "why" behind extending Event 
in AS3.  The reasons become self-evident over time as you use it. 
There's no reason not to, so best to just do so.


The only reason you wouldn't is if you spent considerable time to deeply 
understand the event system in AS3 (reading posts is only part of it - 
you would need to spend time running tests, poking around in the code, 
doing R&D, basically) and somehow determined that extending Event was 
detrimental to a specific edge case that you were working on.  I'd be 
hard pressed to figure out what that edge case would be, and I could 
come up with several arguments against the decision to not extend Event 
in that case.


In other words, if you have to ask why you should use it, you should use 
it.  ;)

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Why is good to extend an Event Class

2008-05-06 Thread Glen Pike

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 

Re: [Flashcoders] Why is good to extend an Event Class

2008-05-06 Thread Allandt Bik-Elliott (Receptacle)
i used a scrollbar class from the kirupa forum that extended the  
event class to accept the amount (in percent) that the bar was  
scrolling by - this allowed both the scrollbar and the scrolling  
object to receive the percent by listening to the event




On 6 May 2008, at 17:21, 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 any good tutorials/articles on this?
I am a visual learner and I don't get it fully just yet..
Meanwhile I'll keep searching..

Dwayne
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders




___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] Why is good to extend an Event Class

2008-05-06 Thread Mendelsohn, Michael
There is an excellent thorough chapter on this in the Moock's AS3 book.
Well worth reading.

- MM




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 any good tutorials/articles on this?
I am a visual learner and I don't get it fully just yet..
Meanwhile I'll keep searching..

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders