0

I have been struggling with the following for a couple of hours now, how do you call a custom class from another class if you do not extend that class for example.

I have on my main Base class a timer event, Base.myTimer.addEventListener(TimerEvent.TIMER, processTime) - Base class

Then I remove the method later in the code Base.mytimer.removeEventListener(TimerEvent.TIMER, processTime. - Base class

I have a button (Btn class) that when its done processing I want to call that method again, but I can't get it to work because the method does not exist in the button class but on the Base class so flash obviously gives me the error processTime is not defined.

For example now I want to re-instantiate the event listener from within the button, so I have

Base.myTimer.addEventListener(TimerEvent.TIMER, processTime); or this.parent.parent["myTimer"].addEventListener()

myTimer is a static Timer in Base class.

I can make a normal dispatchEvent if its not a custom method for example Base.myTimer.dispatchEvent(new TimerEvent(TimerEvent.TIMER)).

The examples I have seen so far have not resolved my issue. Any help would be appreciated.

2 Answers2

0

You could pass a reference to an instance of Base class into your Button instance.

// Button class
package {
    import Base;
    // Other imports...

    public class Button {
        public function Button(base:Base):void {
            // processTime and myTimer must be public.
            // I put this line in the constructor for brevity, but if you stored base 
            // in an instance variable, you could put this anywhere in the button 
            // class.
            Base.myTimer.addEventListener(TimerEvent.TIMER, base.processTime)
        }
    }
}

// Create button like this. 
var button:Button = new Button(base);

// Or if button is created inside of Base
var button:Button = new Button(this);

Even better would be to create two methods in the Base class, for adding and removing the listeners, and make myTimer and processTime private:

public class Base {
    public function addTimerListeners():void {
        myTimer.addEventListener(TimerEvent.TIMER, processTime)
    }

    public function removeTimerListeners():void {
        myTimer.removeEventListener(TimerEvent.TIMER, processTime)
    }
}

Then you can call these two methods from outside of the class. This keeps the inner workings of your class more hidden. If you decided you wanted to change myTimer to a instance variable instead of static, you wouldn't have to make any changes to code outside of your Base class. This is called encapsulation and is a good practice.

bwroga
  • 5,379
  • 2
  • 23
  • 25
0

Looks like the button class is part of the child tree of the Base class. In this case, you can just do a dispatchEvent from the button class when it is clicked

dispatchEvent(new Event("AddListenerAgain", true));

In the Base class, you must be having access to the button class already, hence you can say:

button.addEventListener("AddListenerAgain", addListener);

Then in the Base class

private function addListener(e:Event) : void {
 myTimer.addEventListener(TimerEvent.TIMER, processTime);
}

In this example, I have dispatched and listened for raw strings. This is not a recommended practice. You must read on how to dispatch custom events to do it correctly.

abnvp
  • 1,037
  • 11
  • 19