ActionScript3: Changing button text

James T picture James T · Apr 24, 2011 · Viewed 15.8k times · Source

I have a button instance named Button that I have in my movie, this instance has a Dynamic Text object in it named myText, how can I change the text? I already tried Button.myText.text = "Stuff";

I get the error "Scene 1, Layer 'Layer 1', Frame 1, Line 7 1119: Access of possibly undefined property myText through a reference with static type flash.display:SimpleButton." When I compile.

AS:

import flash.events.MouseEvent;

TheButton.addEventListener(MouseEvent.CLICK, onClick);

function onClick(Event:MouseEvent):void{
    TheButton.myText.text = "Moo";
}

Answer

Taurayi picture Taurayi · Apr 24, 2011

You can't use the dot syntax to access a display object container's child display objects in AS3 as you did in AS2. Normally you would use the display object container's getChildByName() method to get its child display objects, but because your dealing with an instance of SimpleButton which is a subclass of DisplayObject that method doesn't exist. The simple solution is to change you button from a button to a movieclip after which the following should work:

TheButton.addEventListener(MouseEvent.CLICK, onClick);

function onClick(Event:MouseEvent):void
{
    TheButton.getChildByName("myText").text = "Moo";

}

Note: the TextField display object in the TheButton display object container must have an instance name of "myText" and obviously the TheButton display object container must have an instance name of "TheButton".

Also if your going with this approach you may want to rewrite the code as follows:

import flash.display.DisplayObjectContainer
import flash.events.MouseEvent;
import flash.text.TextField;

button.addEventListener(MouseEvent.CLICK, onButtonClick);

function onButtonClick(e:MouseEvent):void
{
    var button:DisplayObjectContainer = DisplayObjectContainer(e.target);
    var textField:TextField = TextField(button.getChildByName("textField"));
    textField.text = "Moo";

}// end function

[UPDATE]

Another solution is to create a movieclip/sprite object for the SimpleButton object's upState, overState and downState properties like the following:

import flash.display.DisplayObjectContainer;
import flash.display.SimpleButton;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;

var simpleButton:SimpleButton = new SimpleButton();
addChild(simpleButton);

var content:Sprite = new Sprite();
draw(content.graphics, 0xFF0000);

var textField:TextField = new TextField();
textField.name = "textField";
textField.text = "UP";
content.addChild(textField);

simpleButton.upState = content;
simpleButton.overState = content;
simpleButton.downState = content;
simpleButton.hitTestState = content;

simpleButton.addEventListener(MouseEvent.MOUSE_OVER, onSimpleButtonMouseOver);
simpleButton.addEventListener(MouseEvent.MOUSE_DOWN, onSimpleButtonMouseDown);
simpleButton.addEventListener(MouseEvent.MOUSE_OUT, onSimpleButtonMouseOut);
simpleButton.addEventListener(MouseEvent.MOUSE_UP, onSimpleButtonMouseUp);

function onSimpleButtonMouseOver(e:MouseEvent):void
{
    var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).overState);
    var textField:TextField = TextField(content.getChildByName("textField"));
    textField.text = "OVER";
    draw(content.graphics, 0xC8C8C8);

}// end function

function onSimpleButtonMouseDown(e:MouseEvent):void
{
    var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).downState);
    var textField:TextField = TextField(content.getChildByName("textField"));

    textField.text = "DOWN";
    draw(content.graphics, 0x646464);

}// end function

function onSimpleButtonMouseUp(e:MouseEvent):void
{
    var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).overState);
    var textField:TextField = TextField(content.getChildByName("textField"));

    textField.text = "OVER";
    draw(content.graphics, 0xC8C8C8);

}// end function

function onSimpleButtonMouseOut(e:MouseEvent):void
{
    var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).upState);
    var textField:TextField = TextField(content.getChildByName("textField"));

    textField.text = "UP";
    draw(content.graphics, 0xFF0000);

}// end function

function draw(graphics:Graphics, color:uint):void
{
    graphics.clear();
    graphics.beginFill(color)
    graphics.drawRect(0, 0, 100, 100);
    graphics.endFill();

}// end function