Skip to main content
Participant
June 19, 2009
Answered

Having trouble utilizing event functions in a class.

  • June 19, 2009
  • 1 reply
  • 705 views

I am trying to write button code in a class, so my main actions panal of my .fla doesn't get too overcrowded. Here is my class coding:

package grid {
import flash.display.*;
import flash.events.*;

public class g1x1 extends MovieClip {
  public function activateGrid() {
   this.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
  }
  function onMouseClickEvent(event:Event):void {
   trace("mouse clicked on me");
  }
}
}

Then on my main timeline I call to that class using:

instance:g1x1 = new g1x1();
instance.activateGrid();

I have also tryed linking the button, using export for actionscript, but I don't know what I'm doing wrong I keep getting errors when I try to compile.

I know something is wrong with my code, I'm used to assigning code right onto the buttons in AS2.

Can sombody tell me what I'm doing worng?

Thanks

Matt.

This topic has been closed for replies.
Correct answer funkysoul

still gives me the same error.

does anyone know why I keep getting the 1180: Call to a possibly undefined method error?


This is how it should look like:

Class:

package grid {
import flash.display.*;
import flash.events.*;

public class g1x1 extends MovieClip {

public function g1x1()

{

}
public function activateGrid()

{
   this.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
}
private function onMouseClickEvent(event:Event):void

{
   trace("mouse clicked on me");
   }
  }
}

your document class or timeline or .as

import grid.*;

private var myGrid1x1:g1x1 = new g1x1();

myGrid1x1.activateGrid();

1 reply

Participating Frequently
June 19, 2009

public class g1x1 extends MovieClip {

     // Add this

     public function g1x1()

     {

          super();

     }


  public function activateGrid() {
   this.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
  }
  // Make this private or protected

private function onMouseClickEvent(event:Event):void {
   trace("mouse clicked on me");
  }
}

The rest looks okay.

Participant
June 19, 2009

i still get an error when compiling:

Description:

1120: Access of undefined property g1x1.  

Source:

instance:g1x1 = new g1x1();

funkysoul
Inspiring
June 19, 2009

you are calling your class wrong:

"instance:g1x1 = new g1x1();"

it should be

"g1x1 = new g1x1();"

you can add g1x1 to your instance with addChild(g1x1);