Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-28-2006, 06:46 PM   #1
FarFeTTu
www.casasoft.com.mt
 
FarFeTTu's Avatar
 
Join Date: May 2003
Location: Malta
Posts: 123
Send a message via MSN to FarFeTTu
Question Possible to dispatch event within a class?

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:Numberspeech: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 1layer=actionsframe=1:Line 35There is no method with the name 'addEventListener'.
     
mySettings.addEventListener("onFontChange",myListener);

Total ActionScript Errors1      Reported Errors
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?
__________________

Last edited by FarFeTTu; 04-28-2006 at 06:48 PM.
FarFeTTu is offline   Reply With Quote
Old 04-28-2006, 07:24 PM   #2
deadbeat
Registered User
 
Join Date: Feb 2001
Location: vancouver
Posts: 2,219
Default

See:

http://www.gskinner.com/blog/archives/000023.html

K.
deadbeat is offline   Reply With Quote
Old 04-28-2006, 07:34 PM   #3
kouri
Senior Member
 
kouri's Avatar
 
Join Date: Jul 2002
Posts: 103
Default

Hi Farfettu,

Try to replace your absolute path mx.events.EventDispatcher by an import above your class; you should like it

Code:
import mx.events.EventDispatcher;
class Settings{
....
 public function Settings(fontSize:Number, speech:Boolean) { 
        //initialize the class so it can dispatch events (also found in help) 
        EventDispatcher.initialize(this); 
....
Viva Valletta !!

HTH
__________________
Kouri
- Hé tu m’aides pas là ? - Non pas là. (Les Bronzés font du ski)

Last edited by kouri; 04-28-2006 at 07:47 PM.
kouri is offline   Reply With Quote
Old 04-28-2006, 07:44 PM   #4
FarFeTTu
www.casasoft.com.mt
 
FarFeTTu's Avatar
 
Join Date: May 2003
Location: Malta
Posts: 123
Send a message via MSN to FarFeTTu
Default

Thanks a lot for your help. I'll check them out when I'm back home and will give you my response.

You were very fast Really appreciated
__________________
FarFeTTu is offline   Reply With Quote
Old 04-28-2006, 07:45 PM   #5
deadbeat
Registered User
 
Join Date: Feb 2001
Location: vancouver
Posts: 2,219
Default

Also, declare the addEventListener and removeEventListener functions:

private var addEventListener :Function;
private var removeEventListener :Function;

K.
deadbeat is offline   Reply With Quote
Old 04-29-2006, 11:09 AM   #6
FarFeTTu
www.casasoft.com.mt
 
FarFeTTu's Avatar
 
Join Date: May 2003
Location: Malta
Posts: 123
Send a message via MSN to FarFeTTu
Default

Ok I managed to make the dispatch event work with your help!

Thanks a lot for your help, my problem was as deadbeat said.. I had to decalre the addEventListener/removeEventListener function because that was why it was giving me the error that the method did not exist.

Thank you guys!!
__________________
FarFeTTu is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the mx.util.Delegate class within your classes! madgett ActionScript 2.0 3 09-02-2007 11:11 PM
problem: cuePoint event properties in class, scope, delegates atomicjoe ActionScript 2.0 2 12-29-2004 06:13 PM
Database simulated with arrays on a cd-rom lecasn5 Components 61 09-07-2004 11:40 AM
getting all obejcts in a class to listen for event mistermikeb ActionScript 2.0 4 07-19-2004 04:27 PM


All times are GMT. The time now is 04:03 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.