3

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";
}
James T
  • 3,292
  • 8
  • 40
  • 70

4 Answers4

3

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
Taurayi
  • 3,209
  • 2
  • 17
  • 15
  • `Scene 1, Layer 'Layer 1', Frame 1, Line 9 1119: Access of possibly undefined property text through a reference with static type flash.display:DisplayObject.` Still gives a error, using your second block of code. – James T Apr 24 '11 at 16:54
  • getChildByName is implemented by DisplayObjectContainer. SimpleButton is not a DisplayObjectContainer. – Andrew Traviss Apr 25 '11 at 00:01
  • @Andrew Traviss but fl.controls.Button does, so I assume he is using that instead. Or perhaps he is using a `MovieClip` object. – Taurayi Apr 25 '11 at 00:34
  • @Andrew Traviss to be honest I didn't even realize till now that he was using a `SimpleButton`, I just assumed it was a movie clip on stage. Now I'm curious as to why he accepted my answer, unless he went with using a movie clip which would make my answer correct. – Taurayi Apr 25 '11 at 00:46
  • @Taurayi you'll note that the error message listed names SimpleButton, so I took my cue from that. – Andrew Traviss Apr 25 '11 at 02:51
  • @Andrew Traviss now that I know he was using an instance of `SimpleButton`, I've altered and updated my question so that it is correct. – Taurayi Apr 25 '11 at 13:45
1

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!

0

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.

roberttdev
  • 42,762
  • 2
  • 20
  • 23
  • "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." I still get that error after renaming the button. – James T Apr 24 '11 at 14:11
  • Yeah, the "static" part of the reference sounds like it thought you were accessing a class property instead of an instance property. What's the new name you used? Where are you defining the instance name? – roberttdev Apr 24 '11 at 14:20
  • I click on the button instance on the main stage, and I named it TheButton. Then inside the button instance, the text's instance name is myText. – James T Apr 24 '11 at 14:22
  • If you put a breakpoint in the click handler and check out the "TheButton" object, do you see "myText" as a property of it in the debugger? It may be that myText was accidentally set up in a way that Flash doesn't consider it a child of TheButton. – roberttdev Apr 24 '11 at 14:31
  • Nope, I can't see it. Any idea why? – James T Apr 24 '11 at 14:41
  • It may look as if it's a child of the Button, but it's not actually. Try deleting it and then editing the button specifically (so everything else is greyed out).. then adding it back in so that it's considered "inside" the button definition. – roberttdev Apr 24 '11 at 14:59
  • I tried removing it and re-adding it, didn't work, can you look at it? http://www.xivio.com/bubby/getitem.fla – James T Apr 24 '11 at 15:06
  • Ah, yes.. seems I made a bad assumption about your object. You can't add children to a Button, as it doesn't inherit the capability to support children (for a deeper explanation, check out http://picometric.blogspot.com/2009/03/actionscript-why-cant-you-access.html). You will have to set up a MovieClip that acts as a button instead if you want to dynamically manipulate children. It's not hard, but there are also some tutorials around online (and sample code) that you can use. – roberttdev Apr 24 '11 at 15:11
0

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.

Andrew Traviss
  • 249
  • 1
  • 7