Is there a way where you can dispatch an event from a class? Like for example, with the LoadVars object you can listen to the onLoad event and the event triggers when the object has loaded. I want to do something like that. I have searched a lot and tried but all in vain
This is my problem:
I have created a class named Settings and with this class I create an object with two members, fontSize and speech. Font size is a number whilst speech is a boolean. I have defined the getter/setter methods for both the fontSize and speech members of the class. What I need to do is that when I change the value of the fontSize or speech, an event is dispatched. I have searched on dispatching events and this is what I am doing to dispatch an event. See my code listed below for the class Settings.as
PHP Code:
class Settings {
//private members for the class Settings
private var _fontSize:Number;
private var _speech:Boolean;
//Declare the dispatchEvent method as a member of this class (found in help)
private var dispatchEvent:Function;
//Constructor method
public function Settings(fontSize:Number, speech:Boolean) {
//initialize the class so it can dispatch events (also found in help)
mx.events.EventDispatcher.initialize(this);
this._fontSize = fontSize;
this._speech = speech;
}
//Getter method for fontSize member
public function get fontSize():Number {
return _fontSize;
}
//Setter method for fontSize member
public function set fontSize(nFontSize:Number):Void {
_fontSize = nFontSize;
//Here i want to dispatch an event since the font size member has changed.
//This is the way I found out about dispatching events
dispatchEvent({type:"onFontChange", target:this});
}
//Getter method for speech member
public function get speech():Boolean {
return _speech;
}
//Setter method for speech member
public function set speech(bSpeech:Boolean):Void {
_speech = bSpeech;
//Here i want to dispatch an event since the speech member has changed.
//This is the way I found out about dispatching events
dispatchEvent({type:"onSpeechChange", target:this});
}
}
Now after having created that class, this is the way I am listening to the event I should be dispatching once the Settings object have been created and one property of the object have changed:
PHP Code:
//Creating the Settings object
var mySettings:Settings = new Settings(12,true);
//Creating the listener object
var myListener:Object = new Object();
//Setting the object to listen to the onFontChange event
myListener.onFontChange = function(oEvent:Object):Void {
trace("New Font Size: " + oEvent.target.fontSize);
trace("Speech is: " + oEvent.target.speech);
}
mySettings.addEventListener("onFontChange",myListener);
mySettings.fontSize = 10;
When I am compiling the swf, it is giving me this error:
PHP Code:
**Error** Scene=Scene 1, layer=actions, frame=1:Line 35: There is no method with the name 'addEventListener'.
mySettings.addEventListener("onFontChange",myListener);
Total ActionScript Errors: 1 Reported Errors: 1
Is there any one can help me with dispatching events within a class? Else inform me if it is possible to do so or not?