我的电影中有一个名为 Button 的按钮实例,该实例中有一个名为 myText 的动态文本对象,如何更改文本?我已经尝试过 Button.myText.text = "Stuff";
我收到错误消息“场景 1,第 1 层,第 1 帧,第 7 行 1119:通过静态类型 flash.display:SimpleButton 的引用访问可能未定义的属性 myText”。当我编译。
作为:
import flash.events.MouseEvent;
TheButton.addEventListener(MouseEvent.CLICK, onClick);
function onClick(Event:MouseEvent):void{
TheButton.myText.text = "Moo";
}
最佳答案
您不能像在 AS2 中那样使用点语法在 AS3 中访问显示对象容器的子显示对象。通常你会使用显示对象容器的 getChildByName()
方法来获取它的子显示对象,但是因为你处理的是 SimpleButton
的一个实例,它是 DisplayObject
的子类,该方法不存在。简单的解决方案是将您的按钮从按钮更改为影片剪辑,之后以下内容应该可以工作:
TheButton.addEventListener(MouseEvent.CLICK, onClick);
function onClick(Event:MouseEvent):void
{
TheButton.getChildByName("myText").text = "Moo";
}
注意:
TextField
显示对象容器中的 TheButton
显示对象的实例名称必须为“myText”,显然 TheButton
显示对象容器的实例名称必须为“TheButton”。此外,如果您采用这种方法,您可能需要按如下方式重写代码:
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
[更新]
另一种解决方案是为
SimpleButton
对象的 upState
、 overState
和 downState
属性创建一个影片剪辑/ Sprite 对象,如下所示: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
关于actionscript-3 - ActionScript3 : Changing button text,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5770905/