PDA

View Full Version : AIR How to define events?



Tahereh
December 2nd, 2009, 02:48 AM
Hi all

1. Adobe's documentation says :

"Events in ActionScript aren’t defined using a specific syntax. Instead, you define events in your class by using the functionality of the EventDispatcher class to keep track of event listeners and notify them of events."
But in some Adobe's sample files, events are defined as class properties or local variables in class methods. while we call the dispatchevent() method only one time for each event (By the way is it possible to dispatch a single event more than one time?:ponder:) and do not use the event object elsewhere in the class, what is the advantage of assigning the event to a variable? specially to a class property, specially to a public class property, while we can define and dispatch the event in a single statement like this:
this .dispatchEvent(new Event(Event.CHANGE,true)) ?


2. The AS3 Language Reference says :

"You do not normally call clone(); the EventDispatcher class calls it automatically when you redispatch an event—that is, when you call dispatchEvent(event) from a handler that is handling event. "
Why in some Adobe's sample files the clone of an event is used as the parameter for dispatchEvent() method ( dispatchEvent(event.clone()) ) instead of the event itself?

Tahereh
December 2nd, 2009, 11:43 AM
:whistle2:...........:whistle2:...........

wvxvw
December 2nd, 2009, 11:59 AM
is it possible to dispatch a single event more than one time?
<< This is not possible with regular events which are of type flash.events.Event and like and built-in EventDispatcher class. by default the EventDispatcher will clone event when passed to it, so, it may be a wise choice to store an event inside the class as a property to feed it to the dispatcher every time you want to dispatch it.
Another way to avoid creating new instances in custom events - you may override clone with this signature:

public override function clone():Event { return this; }
However, bear in mind the consequences, such events will carry on whatever they gather when being dispatched, and this is not the best approach for public API, however, for internal use, I'd sure use this approach rather then returning a new instance of an event.

Why in some Adobe's sample files...
>> That specific line doesn't make sense, can you pleas tell where did you find that?

Tahereh
December 3rd, 2009, 06:51 AM
Thank you so much "wvxvw" ;

1.



by default the EventDispatcher will clone event when passed to it, so, it may be a wise choice to store an event inside the class as a property to feed it to the dispatcher every time you want to dispatch it.


Why? While the dispatchevent() method is called only once for each event and consequently its relevant property will be used only once throughout the class(private property) and you don't want the event to be accessible from outside the class (public prop.)



Another way to avoid ....

Thank you, cool tip. But my question was about built-in events.


2.




package com.example.programmingas3.podcastplayer
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.events.TimerEvent;
import flash.media.ID3Info;
import flash.media.So



............
public function onLoadOpen(event:Event):void {
if (this.isStreaming) {
this.isReadyToPlay=true;
if (autoPlay) {
this.play();
}
}
this.dispatchEvent(event.clone());
}
public function onLoadProgress(event:ProgressEvent):void {
this.dispatchEvent(event.clone());
}

public function onLoadComplete(event:Event):void {
this.isReadyToPlay=true;
this.isLoaded=true;
this.dispatchEvent(event.clone());
// if the sound hasn't started playing yet, start it now
if (autoPlay&&! isPlaying) {
play();
}
}



.......



public function onPlayTimer(event:TimerEvent):void {
var estimatedLength:int=Math.ceil(this.s.length/this.s.bytesLoaded/this.s.bytesTotal);
var progEvent:ProgressEvent=new ProgressEvent(PLAY_PROGRESS,false,false,this.sc.po sition,estimatedLength);
this.dispatchEvent(progEvent);
}

}
}

wvxvw
December 3rd, 2009, 07:26 AM
Well, if you will try to profile your app in Flex profiler, you'll see the figures yourself, events are created in enormous quantities... This is precisely due to the clone() being called each time event is dispatched. They are GCed, so, this doesn't lead to memory leaks, but reusing old events would be a performance optimization anyway... besides, if the number of objects that need to be swept by GC is high, you'll see some jerking of the player at the time the GC kicks in, if that happens during an animation - it just doesn't look good :)

I think that the reason is buried somewhere in the stopPropagation() event functionality that may require a new copy of an event rather then reusing the same event... However, I cannot come up with the workflow where reusing the same event will break the functionality, but, I may not consider all the cases... (read as I may be wrong here)

EDIT: And, oh, well, just thought of it! Bubbling events for sure will require cloning because they have to reset target and currentTarget properties. So, I think that if you design an event that shouldn't bubble, then whence you override the clone() method your best bet is to return the refernce to the already existing event.