开发者

Button Class by instance name, Actionscript-3

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);
         s开发者_开发问答tage.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


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:


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);//<--------------------------
         }
    }  
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜