views:

137

answers:

3

Hey
I'm so use to timeline code, that this baffles me. How do I get a button class to recognize a button instance on stage?
Please help.

...Just revised
Buttons.fla

Class: 'Buttons'
Button with instance name placed on stage

Buttons.as

package {
    import flash.display.MovieClip;
    public class Buttons extends MovieClip {

         public function Buttons() {
         mc.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
         stage.addChild(this);

         }

         public function onClick(event:MouseEvent):void{
             trace("Hello World");
         }
    }  
}

Error:
1120: undefined property
The error indicates it's the mouse event, not my instance name mc.

LINK TO THE FILE

+2  A: 

You are missing a curly brace and a definition of mc and an import of MouseEvent (the root of your problem above):

package {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class Buttons extends MovieClip {

         public function Buttons() {
             //it's better to use "this" here instead of adding another
             //instance of movieclip named "mc"
             this.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
         }

         public function onClick(event:MouseEvent):void{
             trace("Hello World");
         }
    }  
}

Of course, there are several other/better ways to accomplish these same results, but that should at least fix your compile problem. Now, to get this on stage, you need to add it to something that exists. One simple way to do that is by putting the following line right below this.addEventListener:

stage.addChild(this);

If you have other questions on getting this to work, let me know. I hope that points you in the right direction,

--gMale

EDIT:

In response to the comments below here is a link to the Flash files. I tried to follow the intent of what you were doing. There's one quick clickable button coded within the IDE and one quick clickable button coded in a separate *.AS file:

gmale
@gmale, I want to give markups to anyone who can help. Your suggestion creates a #1046 error 'not a compile time constant' I need the instance name to work with the stage from my fla.
pixelGreaser
I made a link to the file:)
pixelGreaser
Ok. I'll open your file in my ide and get it to compile and update my post...
gmale
@pixelGreaser: link posted above. Let me know if you have any questions. The code above compiled fine. Perhaps you left out the MouseEvent import, maybe? Or maybe the fla file was still pointing to an older version of the ActionScript file??? I'm not sure.
gmale
+1  A: 

This may help with the 'mc' instance

package {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    public class Buttons extends MovieClip {
         public function Buttons() {
           //it's better to use "this" here instead of adding another
           //instance of movieclip named "mc"
         this.addEventListener(MouseEvent.MOUSE_DOWN, onClick); 
         }
         public function onClick(event:MouseEvent):void{
           trace("Hello World");
         //PASS IT INTO THE BRACKETS
         stage.addChild(mc);//<--------------------------
         }
    }  
}
VideoDnd
A: 

Hello,

to access instances created in the IDE you need to call them with the [] syntax, this works :

package {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    public class Buttons extends MovieClip {

         public function Buttons() {
         this["mc"].addEventListener(MouseEvent.MOUSE_DOWN, onClick);
         //stage.addChild(this); // this is really not useful
         }
         public function onClick(event:MouseEvent):void{
             trace("Hello World");
         }
    }  
}

also note you need to import MouseEvent. :)

if you really need to be able to access your button via mc, it needs more code :

package {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    public class Buttons extends MovieClip {

         protected var mc:MovieClip;

         public function Buttons() {

            if(this["mc"] is MovieClip){
              mc = this["mc"];
            }else{
              //you probably want to create it if not found on the stage.
            }
         mc.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
         }
         public function onClick(event:MouseEvent):void{
             trace("Hello World");
         }
    }  
}

Hope this helps.

Boris