James T
James T

Reputation: 3320

ActionScript3: Changing button text

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

Upvotes: 3

Views: 16233

Answers (4)

theawesomecoder61
theawesomecoder61

Reputation: 35

It's extremely easy.

Suppose I have a button that was created from the Components menu called myBtn.

myBtn.label = "Awesome text";

You use label instead of creating a textfield over it then setting the texfield's text.

That's it!

Upvotes: 1

Taurayi
Taurayi

Reputation: 3207

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

Upvotes: 3

Andrew Traviss
Andrew Traviss

Reputation: 249

Children of SimpleButton are inaccessible from Actionscript; notice that it does not actually extend DisplayObjectContainer. The SimpleButton class is a bit of a mutant.

You will have to use a container MovieClip and put the SimpleButton and the label TextField inside. Alternatively you can roll your own button out of a MovieClip or use a component library. SimpleButton is not meant for this kind of use.

Upvotes: 0

roberttdev
roberttdev

Reputation: 46633

In the absence of extra information as to whether you're getting errors and what they are, my first assumption would be that the system is having trouble with the variable name "Button". There is already a class named "Button", so I would think the system is thinking you are trying to call a class-level method/var of class Button instead of accessing a variable named "Button". I would try changing the name of the button var to something unique.

Upvotes: 0

Related Questions