2

I'm writing a program using Main.as, that needs to listen to a function (getColor) in another class file (GetColor.as). I have the following in GetColor.as:

public class GetColor
{
  public function getColor(event:MouseEvent):void
  {
    //doing stuff here
    this.dispatchEvent(new Event("changeColor") );
  }
}

and then in Main.as I have:

var getPicColor:GetColor = new GetColor();
getPicColor.addEventListener("changeColor",changeColorNow);
function changeColorNow(e:Event):void
{
  //do stuff here
}

However, I am getting an error:

1061: Call to a possibly undefined method dispatchEvent through a reference
with static type GetColor.

What does this mean? I have nothing declared as static. Am I supposed to create an instance of dispatchEvent(), as opposed to using "this"?

UMich_2017
  • 131
  • 9

1 Answers1

2

You cannot dispatch events with a class that (implicitly) extends Object -> that's why you are getting there error -> where is "dispatchEvent()" method coming from? Where is it inherited from? (answer: it is not!)

Your GetColor class (horrible name there! :) ) must either extend a display object - which in your case it not really the correct solution, extend EventDispatcher or implement IEventDispatcher.

Then you can use the method dispatchEvent.

Fygo
  • 4,555
  • 6
  • 33
  • 47
  • If GetColor is something like a color picker component extending a `DisplayObject` can be a correct solution. – null May 07 '15 at 17:35