Question

I have a "handmade" button (_btn as instance name), with only a dynamicText inside (or can I say one dynaticText for each of the four button modes). The dynamicText for Up mode has an instance name of "_text". So if I am working on the AS of the movieClip that contains the button, how can I address the text of the dynamic text?

package  {
import flash.display.MovieClip;
import flash.display.SimpleButton;

public class container extends MovieClip {
    var _btn:button;

    public function container() {
        _btn= new button;
        this.addChild(_btn);
        switch (_btn._text.text)
        {
            case "a": doAFunction();
            case "b": doBFunction();
            case "c": doCFunction();
        }

    }

}

}

What is the problem with my code?

Access of possibly undefined property _text through a reference with static type flash.display:SimpleButton.
Was it helpful?

Solution

When you create a button symbol in flash professional, the symbol becomes an instance of SimpleButton. If you want to access the display objects that you've created in the button symbol, e.g dynamic textfield, you have to access it via the SimpleButton object's upState, overState, downState and hitTestState properties.

I've created a utility function that you can include in your project to get dynamic textfields. The function is called getDTFFromSB()(get dynamic textfield from simplebutton).

The function takes the arguments simpleButton and state. The simpleButton argument is obviously for the SimpleButton object in question, and the state argument is for specifying which state property(upState,overState,downState and hitTestState) the textfield is located on with the respective string values: "up", "over", "down" and "hit".

The function returns an Object object that is either a TextField object, Vector.<TextField object or null.

Here is the utility function getDTFFromSB():

package utils
{
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.SimpleButton;
    import flash.text.TextField;

    /**
     * get dynamic textfield from simple button
     */
    public function getDTFFromSB(simpleButton:SimpleButton, state:String = "up"):Object
    {
        var returnObject:Object;

        var displayObject:DisplayObject;

        switch(state)
        {
            case "up" : displayObject = simpleButton.upState;
            case "over" : displayObject = simpleButton.overState;
            case "down" : displayObject = simpleButton.downState;
            case "hit" : displayObject = simpleButton.hitTestState;

        }// end switch

        if(displayObject is TextField)
        {
            returnObject = displayObject;
        }
        else if(displayObject is DisplayObjectContainer)
        {
            var doc:DisplayObjectContainer = displayObject as DisplayObjectContainer;
            var textFields:Vector.<TextField> = new Vector.<TextField>();

            for(var i:uint = 0; i < doc.numChildren; i++)
            {
                if(doc.getChildAt(i) is TextField) 
                textFields.push(doc.getChildAt(i));

            }// end for

            if(textFields.length == 0) returnObject = null
            else if(textFields.length == 1) returnObject = textFields[0]
            else if(textFields.length > 1) returnObject = textFields;

        }
        else
        {
            returnObject = null;

        }// end else

        return returnObject;

    }// end function

}// end package

I also created an example of its use. In the library I have a button symbol that I exported for actionscript. The button symbol has one dynamic textfield in its "up" state and its text property's value is "a". Here is the code in the document class:

package
{
    import utils.getDTFFromSB;
    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;

    public class Main extends Sprite
    {
        private var _myButton:SimpleButton;

        public function Main()
        {
            if(stage) init()
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            _myButton = new MyButton();
            addChild(_myButton);

            var returnedObject:Object = getDTFFromSB(_myButton);

            var textField:TextField;

            if(returnedObject is TextField)
            {
                textField = TextField(returnedObject);

            }
            else if(returnedObject is Vector.<TextField>)
            {
                textField = Vector.<TextField>(returnedObject)[0];

            }
            else if(returnedObject == null)
            {
                throw new Error("null value returned from getDTFFromSB() method");

            }// end if

            trace(textField.text) // ouput: a

        }// end function

    }// end class

}// end package

When I create a new instance of the button symbol, called _myButton, I parse it to the getDTFFromSB() function and store the returned object in a local object, called returnedObject. Then I use if statements to determine the type of the return object, and if it is a TextField object, I store it in a localTextFieldobject. Now if I trace thetextproperty of theTextField` object I get "a".

OTHER TIPS

"button" is not known in this script - if you want to create a standard button, use new Button() (with capital B).

and i think Button has no text attribute but one that's called "label".

try using _btn.label.

package  {
  import flash.display.MovieClip;
  //import flash.display.SimpleButton;
  import fl.controls.Button

  public class container extends MovieClip {
    var _btn:button;
    public function container() {
      _btn= new button;
      this.addChild(_btn);
      switch (_btn.label)
      // switch (_btn._text.text)
      {
          case "a": doAFunction();
          case "b": doBFunction();
          case "c": doCFunction();
      }

    }

  }

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