我对flash和as3相对较新。基本上我试图将一个动画片段加载到舞台上,然后单击该动画片段中的一个按钮,从舞台上再次移除该动画片段。我有以下movieclip:
var myResultBox:ResultBox = new ResultBox();
addChild(myResultBox);
我在movieclip中放了一个名为closeButton1的按钮。我试图点击关闭按钮,然后删除动画片段。
MovieClip中的代码是 - //我的结果框中的关闭按钮的事件监听器
closeButton1.addEventListener(MouseEvent.CLICK, closeBMI);
function closeBMI(evt:MouseEvent):void
{
removeChild(myResultBox);
}
以下错误
代码:1120:访问未定义的属性closeButton1。
非常感谢任何帮助
答案 0 :(得分:0)
按钮内?但是你不能以这种方式引用按钮。您应该将代码放在movieclip中,该按钮包含按钮,您可以在其中添加结果addChild(myResultBox)
;因此,您的事件处理程序将能够引用myResultBox
对于按钮内的代码:
this.addEventListener(MouseEvent.CLICK, closeBMI);
function closeBMI(evt:MouseEvent):void
{
//removeChild(myResultBox);
//Sadly, you don't have reference on myResultBox within a button...
}
答案 1 :(得分:0)
下面是一个简单的程序,我认为它具有您要求的功能。据我了解,你在舞台上有按钮A.单击按钮A时,影片剪辑B将添加到舞台中。电影剪辑B中有按钮B.单击按钮B时,将删除影片剪辑B和按钮B.
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
// This creates a movie clip that contains a button.
// This button will remove the movie clip that contains
// it when it is clicked.
var MovieClipB = new MovieClip();
MovieClipB.graphics.lineStyle(1,0);
MovieClipB.graphics.beginFill(0x0000FF,1);
MovieClipB.graphics.drawRect(0,0,50,50);
var ButtonB:MovieClip = new MovieClip();
ButtonB.buttonMode = true;
ButtonB.graphics.lineStyle(1,0);
ButtonB.graphics.beginFill(0xFFFFFF,1)
ButtonB.graphics.drawCircle(0,0,10);
ButtonB.x = ButtonB.y = 25;
MovieClipB.addChild(ButtonB);
ButtonB.addEventListener(MouseEvent.CLICK, removeButtonClickHandler, false, 0, true);
function removeButtonClickHandler(event:MouseEvent):void
{
var button = MovieClip(event.currentTarget);
var container = button.parent;
container.parent.removeChild(container);
}
// This creates a button that starts on the stage.
// When clicked, it adds the movie clip defined above to the stage
var ButtonA:MovieClip = new MovieClip();
ButtonA.buttonMode = true;
ButtonA.graphics.lineStyle(1,0);
ButtonA.graphics.beginFill(0xFF0000,1)
ButtonA.graphics.drawRect(0,0,50,50);
addChild(ButtonA);
ButtonA.x = ButtonA.y = 20;
ButtonA.addEventListener(MouseEvent.CLICK, addButtonClickHandler, false, 0, true);
function addButtonClickHandler(event:MouseEvent) : void
{
addChild(MovieClipB);
MovieClipB.x = 200;
MovieClipB.y = 20;
}