Question

So I have a method that takes in a String and then is suppose to set the dynamic textbox on a button to said String.

public function setText(caption:String) {
  this.btext.text = caption;
}

I really don't understand why this method is producing a 1119 error.
Access of a possibly undefined property btext through a reference with static type Button.as

The instance name of the Dynamic Textbox is btext and I have tried deleting the textbox and making a new one however this still produces a 1119 error. I also read on another stack question that trying this['btext'].text = caption; which gave me plenty of runtime errors.

Basically what am I doing wrong?
Thank you for any help.

EDIT

Here is the code I am using, and I create an instance of button add it to the stage and store it in an array with this code.

Code to create button

this.buttonArray.push(this.addChild(weaponButton));

Button.as

package  {

    import flash.display.MovieClip;
    import flash.filters.*;

    public class Button extends MovieClip {

        public function Button() {

        }

        public function setPosition(xpos:int, ypos:int) {
            this.x = xpos;
            this.y = ypos;
        }

        public function setScale(xScale:Number, yScale:Number) {
            this.scaleX = xScale;
            this.scaleY = yScale;
        }

        public function addDropShadow():Array {
            var dropShadow:DropShadowFilter = new DropShadowFilter(2,45,0, 1,4,4,1,1,true);
            return [dropShadow];
        }

        public function removeDropShadow():Array {
            return null;
        }

        public function setText(caption:String) {
            this.btext.text = caption;
        }
    }

}
Was it helpful?

Solution

As you have stated btext is an instance name of an object. Here is where I assume btext is an object you created in your library.
In your class you are doing 2 things wrong. So lets examine your method.

public function setText(caption:String) {
  this.btext.text = caption;
}

The first thing wrong is you are using "this". "this" is a reference to the current instance of the class you are in. And you are saying btext is a property on said instance. Which as I am assuming it is not because you defined btext as an object in your library. This will give you the property is undefined error you are gettting.

Now the second issue at hand is you are about to ask "OK how do I reference btext in my class then". What you need to know is that only objects added to the display list IE:stage can access objects via the stage.
You can do this 3 ways.
The first way is to pass a reference to the button into the class and store it as a property of the class.
The second way is to add your class to stage and in the class listen to the addedToStage event. At that time you can then access the object.

MovieClip(root)["btext"].text


The first 2 methods are not good practice since btext is not apart of the class and a general rule of thumb would be to encapsulate your class.
To make this work what you could do is have your class assign the value to a property in your class then fire an event and make the parent of this class listen to that event then just grab the value and assign.

Here is some suggested reading

OTHER TIPS

I think the variable btext doesn't exist at all, or is it inherited from Movieclip?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top