开发者

accessing GUI-created elements in a class definition file in AS3 (Flash CS4)

I've used the GUI to create a rectangle that I turned into a button symbol (SimpleButton). I then edited the button so that it had 4 different button states and a text layer on top. I then created a class definition file for this object so that I could dynamically change the label text (the text layer) when adding instances of this button to the stage.

I开发者_如何学Go was able to create and link a class file (DynamicButton.as) just fine, but when I try to access the text field that I created on the button, I get the error:

"Access of possibly undefined property btnLabel through a reference with static type com.examples:DynamicButton."

when i couldn't get that to work, I decided I'd try adding the TextField directly within the class definition file, using the following code:

package com.examples 
{
    import flash.display.Sprite;
    import flash.display.SimpleButton;
    import flash.text.TextField;

    public class DynamicButton extends SimpleButton
    {
        public function DynamicButton(btnText:String="Click Me")
        {
            placeText();
            labelText.text = btnText;           
        }

        //property variables
            //create a text box to hold the button label
            private var labelText:TextField = new TextField();
            //create a displayObject to hold the text
            private var labelSprite:Sprite = new Sprite();      

        private function placeText():void {
            labelText.width = this.width;
            labelText.height = this.height;
            labelText.x = this.x;
            labelText.y = this.y;
            labelText.visible = true;
            labelSprite.addChild(labelText);
            this.parent.addChild(labelSprite);
        }

    }
}

The problem is that I can't seem to add the TextField to the SimpleButton, as it's not a display object. So, I tried adding it to the parent of the simple button (and I figured, I'd just place it exactly above the button). But then I get a "null object reference." error.

So, I have two questions

  1. is there a way to access GUI-created elements from w/i a class definition file?
  2. How would I add the TextField to the button using only AS3 inside of a my class definition file?


OK, it took a few days and some introspection, but it seems that the problem I've having stems from the fact that you cannot add children to a SimpleButton. I changed my class definition to extend a MovieClip, then created a function named "listen()" that I called when constructing the object that added listeners for the "over","down", and "out" mouse states, so as to imitate a simple button. I also had to put a "stop()" command in the constructor, so that each button wouldn't keep cyclying through all the states. The final class definition looks like this:

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

    public class DynamicButton extends MovieClip
    {
        public function DynamicButton(btnText:String="Click Me")
        {
            stop(); 
            this.btnText_btn.text = btnText;
            listen();
        }//constructor  

        private function listen():void {
            this.addEventListener(MouseEvent.MOUSE_OVER,function(){
                    gotoAndStop(2);
                }//anon mouseover fcn
            );
            this.addEventListener(MouseEvent.MOUSE_DOWN,function(){
                    gotoAndStop(3);
                }//anon mousedown fcn
            );
            this.addEventListener(MouseEvent.MOUSE_OUT,function(){
                    gotoAndStop(1);
                }//anon mouseout fcn
            );          
        }
    }//class definition
}//package
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜