0

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

}
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
  • what happens if you leave 'this' away? – 11684 Apr 06 '12 at 20:03
  • where do you declare and initialize 'btext'? – 11684 Apr 06 '12 at 20:07
  • I tried leaving this off and same result. I thought that if you created the textbox in Flash and gave it an instance name that you didn't have to declare or initialize it? If so would you mind posting the code to do so? – Jonny Henly Apr 06 '12 at 20:11
  • I assume you have a class called Button, and that btext is an instance of this class, created like 'btext:Button = new Button;'. But I now realize that the above function could be in the Button class... Could you post the full class? – 11684 Apr 06 '12 at 20:18
  • because the problem is probably outside this function. – 11684 Apr 06 '12 at 20:22
  • sorry, I can't see further comments no my mobile phone... Could you edit the contents of your question to insert the full class? – 11684 Apr 06 '12 at 20:37
  • I know you probably can't see this comment but I updated the question. – Jonny Henly Apr 06 '12 at 20:41
  • Where is btext located? on stage? – The_asMan Apr 06 '12 at 21:01

2 Answers2

2

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

The_asMan
  • 6,364
  • 4
  • 23
  • 34
0

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

11684
  • 7,356
  • 12
  • 48
  • 71