Skip to main content
Known Participant
November 9, 2011
Answered

Access of undefined property (dynamic text box)

  • November 9, 2011
  • 1 reply
  • 4917 views

Hi guys,

I have a movie clip working as a button that is disabled after click. I need to add a scoring option, so that on click it also adds to the score. When I add the scoreGame function, it says the text box is undefined. Ideas?

Here's the code:

package

{

          import flash.display.MovieClip;

          import flash.events.MouseEvent;

          public class DisablingButtons extends MovieClip

          {

                    var labels:Array;

                    var thisParent:*;

                    var score:Number = 0;

                    var goalScore:Number = 8;

                    public function DisablingButtons()

                    {

                              trace("it's working!");

                              labels = this.currentLabels;

                              this.addEventListener(MouseEvent.CLICK, scoreGame);

                              this.addEventListener(MouseEvent.CLICK, disableButton);

                              this.addEventListener(MouseEvent.ROLL_OVER, over);

                              this.addEventListener(MouseEvent.ROLL_OUT, out);

                    }

                    function scoreGame(myEvent:MouseEvent):void

                    {

                              score++;

                              score1.text = String(score);

                              if (score == goalScore)

                              {

                                        gotoAndStop(201);

                              }

                    }

 

                    function disableButton(event:MouseEvent):void

                    {

                              for (var i:int = 0; i < labels.length; i++)

                              {

                                        if (labels.name == "disable")

                                        {

                                                  this.gotoAndPlay("disable");

                                        }

                              }

                              this.removeEventListener(MouseEvent.CLICK, disableButton);

                              this.removeEventListener(MouseEvent.ROLL_OVER, over);

                              this.removeEventListener(MouseEvent.ROLL_OUT, out);

                    }

                    function over(event:MouseEvent):void

                    {

                              for (var j:int = 0; j < labels.length; j++)

                              {

                                        if (labels.name == "over")

                                        {

                                                  this.gotoAndPlay("over");

                                        }

                              }

                    }

                    function out(event:MouseEvent):void

                    {

                              for (var k:int = 0; k < labels.length; k++)

                              {

                                        if (labels.name == "out")

                                        {

                                                  this.gotoAndPlay("out");

                                        }

                              }

                    }

          }

}

This topic has been closed for replies.
Correct answer Ned Murphy

When you instantiate the class in your fla, you would pass the textfield to the class...

import DisablingButtons;

var btn:DisablingButtons = new DisablingButtons(score1);

addChild(btn);

And in your class file you would need to adjust it to accept the textfield such as what I show below.  Just realize that I have manipulated the naming to preserve your score1 name at both ends of your code by naming the argument that the class sees differently (txt).  Otherwise I would have normally used a differewnt variable name within the class than score1

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

          public class DisablingButtons extends MovieClip
          {
                    var labels:Array;
                    var thisParent:*;
                    var score:Number = 0;
                    var goalScore:Number = 8;
                   var score1:TextField;

                     public function DisablingButtons(txt:TextField)
                    {
                              trace("it's working!");
                              score1 = txt;
                              labels = this.currentLabels;
                              this.addEventListener(MouseEvent.CLICK, scoreGame);
                              this.addEventListener(MouseEvent.CLICK, disableButton);
                              this.addEventListener(MouseEvent.ROLL_OVER, over);
                              this.addEventListener(MouseEvent.ROLL_OUT, out);
                    }

etc...

1 reply

Ned Murphy
Legend
November 9, 2011

Where is the TxtField?  If this is not your document class, then you probably need to pass the textfield into this class when you instantiate it.

jianna91Author
Known Participant
November 9, 2011

I've got score1 on the stage, on a separate layer. How would I add the textfield to the class file?

Ned Murphy
Ned MurphyCorrect answer
Legend
November 9, 2011

When you instantiate the class in your fla, you would pass the textfield to the class...

import DisablingButtons;

var btn:DisablingButtons = new DisablingButtons(score1);

addChild(btn);

And in your class file you would need to adjust it to accept the textfield such as what I show below.  Just realize that I have manipulated the naming to preserve your score1 name at both ends of your code by naming the argument that the class sees differently (txt).  Otherwise I would have normally used a differewnt variable name within the class than score1

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

          public class DisablingButtons extends MovieClip
          {
                    var labels:Array;
                    var thisParent:*;
                    var score:Number = 0;
                    var goalScore:Number = 8;
                   var score1:TextField;

                     public function DisablingButtons(txt:TextField)
                    {
                              trace("it's working!");
                              score1 = txt;
                              labels = this.currentLabels;
                              this.addEventListener(MouseEvent.CLICK, scoreGame);
                              this.addEventListener(MouseEvent.CLICK, disableButton);
                              this.addEventListener(MouseEvent.ROLL_OVER, over);
                              this.addEventListener(MouseEvent.ROLL_OUT, out);
                    }

etc...