views:

230

answers:

2

Hi guys, this is my first time trying to use document classes in AS3 and im struggling. I am trying to add event listeners to a 2 levels deep movie clip, waiting for a click however i am getting the following error.

ERROR: Access of undefined property MouseEvent 

package 
{
 import flash.display.MovieClip;
 import flash.media.Sound;
         import flash.media.SoundChannel;

 public class game extends MovieClip
 {
  public var snd_state = true;

  public function game()
  {
   ui_setup();
  }

  public function ui_setup()
  {
   ui_mc.toggleMute_mc.addEventListener(MouseEvent.CLICK, snd_toggle);
  }

  public function snd_toggle(MouseEvent)
  {
   // 0 = No Sound, 1 = Full Sound
   trace("Toggle");
  }
 }
}
A: 

You must import the event: import flash.events.MouseEvent

The function parameter also needs a name: public function snd_toggle(bblabla:MouseEvent) { ...

Bart van Heukelom
A: 

If you're going to use a class, you need to import it. The compiler is telling you that you've referenced the MouseEvent class but didn't include it in your code. I've cleaned it up a bit for you:

package 
{
 import flash.display.MovieClip;
 import flash.events.MouseEvent; // <-- import MouseEvent Class
 import flash.media.Sound;
 import flash.media.SoundChannel;

 public class game extends MovieClip
 {
  public var snd_state:Boolean = true; // -- snd_state is type Boolean

  public function game()
  {
   ui_setup();
  }

  public function ui_setup():void
  {
   ui_mc.toggleMute_mc.addEventListener(MouseEvent.CLICK, snd_toggle);
  }

  private function snd_toggle(event:MouseEvent):void
  {
   // 0 = No Sound, 1 = Full Sound
   trace("Toggle");
  }
 }
}

You'll note that I added :void to the end of your methods. This indicates what type of variable to return. For example, if your method returns a string it would be :String. Also added a paramter to your snd_toggle handler. The parameter is called "event" and it's an instance of class MouseEvent (event:MouseEvent).

Typeoneerror
Thanks the changes worked a treat and i understand where i was going wrong.. I think i have this concept entirly wrong now, i was wondering if you'd mind explaining it.. I assumed that the above class would be instantiated within the first frame on scene one. I also assumed that when changing scenes event listeners etc would still be running.. Scene 1: I have a mc named ui_mc, that has a button in for muting sound.Scene 2: I have the same movie clip with the same button. Now the eventListener picks it up in the first scene, however it does not in the second :S?
Lee
I am attempting to use the same Movie clip to act as a UI overlay in different scenes.. If each of the UI's have the same istance name wouldnt the eventListeners pick up on them regardless of scene?Im sure i have this wrong somewhere..
Lee
Did you set the class as your document class for the FLA?
Typeoneerror
Yea it is set as the document class.
Lee