Skip to main content
July 20, 2009
Question

Dispatching events between different classes

  • July 20, 2009
  • 3 replies
  • 680 views

Im trying to dispatch and event from an XML class.

dispatchEvent(new Event (XMLClass.INDEX_CHANGED));

and catch it in the display class

import AS.XMLClass;

private var _xml  = new XMLClass();

_xml.addEventListener(XMLClass.XML_LOADED, swapImage);

I know that im missing something because the application works

and the function runs but its not dispatching the event the event

or maybe it's not catching the event in the display class, even though everything else is working

and im not gettin' any errors.

This topic has been closed for replies.

3 replies

July 20, 2009

oop is frustrating

kglad
Community Expert
Community Expert
July 20, 2009

are you losing scope in your dataLoaded function?  use trace(this) to see.

July 21, 2009

don't think so,

when i try trace(this) inside the dataloaded function i get [object XMLClass]

im really frustrated, if some one have any other suggestions please let me know!

thanks

kglad
Community Expert
Community Expert
July 20, 2009
in your "other" class:

import AS.XMLClass;

var _xml:XMLClass=new XMLClass();
_xml.dispatchEvent(new Event (XMLClass.INDEX_CHANGED));

in your AS.XMLClass:

addEventListener(XMLClass.XML_LOADED, swapImage);


or vice-versa.  you could dispatch an event from your AS.XML class and assign a listener in your "other" class.

July 20, 2009

that's exactly what i did.

i have 3 classes

AS.XMLClass

AS.ControlClass

AS.DisplayClass

It's like a slideshow thing.

I have a button on stage thats connected to the control class and every time you press it

it changes the index of the image in the xmlclass and everytime that the index is changed the xmlclass needs to

dispatch and event that the index has changed and the display class is going to catch the event and change the image

everything is working except the xmlclass dispatch event or the displayclass catch event

XMLCLASS

public static const INDEX_CHANGED:String = "indexChanged";

private function dataLoaded(event:Event):void{
           
            trace("xml file is loaded");
            data = new XML(loader.data);
            dispatchEvent(new Event(XMLClass.XML_LOADED));
            totalItems = data.images.photo.length();
            setCurrentIndex(currentIndex);
}

CONTROL CLASS

import AS.XMLClass;

private var _xml = new XMLClass();

public function leftButtonClicked(){
            _xml.setCurrentIndex(_xml.currentIndex = _xml.currentIndex - 1);
        }
       
        public function rightButtonClicked(){
            _xml.setCurrentIndex(_xml.currentIndex = _xml.currentIndex + 1);
        }

DISPLAY CLASS

import AS.XMLClass;

private var _xml  = new XMLClass();

_xml.addEventListener(XMLClass.XML_LOADED, swapImage);

public function swapImage(event:Event){
            trace("working....");
        }

kglad
Community Expert
Community Expert
July 20, 2009

is XMLClass.INDEX_CHANGED a string?

an XMLClass instance should dispatch the event and a listener could be defined in your XMLClass.

July 20, 2009

O yea it's a string

public static const INDEX_CHANGED:String = "indexChanged";

Still not working....

how would you change the could to make it work ?