我只是在学习ActionScript 3,所以这对你来说是一个很好的问题。 我通过使用按钮创建了我想要添加到舞台(从库中)的影片剪辑。我总共有六个按钮(和六个moviclips),但我试图让前两个按钮工作,然后再转到下一个按钮。问题是,当单击另一个按钮时,我似乎无法删除影片剪辑,或者相同的按钮......就像我说我是新手,我认为创建了一堆东西......我收到错误2007年现在
import flash.events.MouseEvent;
import flash.display.MovieClip;
mix_btn.addEventListener(MouseEvent.CLICK, addbear);
function addbear(event:MouseEvent):void
{
var movieClip:bear1 = new bear1();
addChild(movieClip);
movieClip.x = 240;
movieClip.y = 45;
mix_btn.removeEventListener(MouseEvent.CLICK, addbear);
mix_btn.addEventListener(MouseEvent.CLICK, removebear);
}
function removebear(event:MouseEvent):void
{
var movieClip:bear1 = null;
removeChild(movieClip);
mix_btn.removeEventListener(MouseEvent.CLICK, removebear);
/* mix_btn.addEventListener(MouseEvent.CLICK, addbear);*/
}
shake_btn.addEventListener(MouseEvent.CLICK, addbear3);
function addbear3(event:MouseEvent):void
{
var movieClip:bear3 = new bear3();
addChild(movieClip);
movieClip.x = 240;
movieClip.y = 45;
shake_btn.removeEventListener(MouseEvent.CLICK, addbear3);
shake_btn.addEventListener(MouseEvent.CLICK, removebear3);
}
function removebear3(event:MouseEvent):void
{
var movieClip:bear3 = null;
removeChild(movieClip);
shake_btn.removeEventListener(MouseEvent.CLICK, removebear3);
shake_btn.addEventListener(MouseEvent.CLICK, addbear3);
}
答案 0 :(得分:2)
var movieClip:bear1 = null;
removeChild(movieClip);
mix_btn.removeEventListener(MouseEvent.CLICK, removebear);
这部分,你基本上尝试删除一个空对象,它通常是坏的:)。这适用于“蛇”和其他按钮或动画片段。如果你想删除你点击的熊,试试这个:
var movieClip:bear1 = bear1(event.currentTarget);
removeChild(movieClip);
mix_btn.removeEventListener(MouseEvent.CLICK, removebear);
答案 1 :(得分:0)
问题在于您将movieClip作为函数级变量引用,该函数在该函数完成时消失。当您的删除功能稍后发生时,movieclip本身仍然存在,但您用来引用它的movieclip变量已经消失。
如果remove事件直接在bear上,您可以使用event.currentTarget来访问和删除它。
相反,由于看起来您的侦听器位于单独的按钮上,因此您需要对可在任何位置工作的动画片段的引用。如果为影片剪辑指定实例名称,则可以使用该名称稍后访问和删除它。
function addbear(event:MouseEvent):void
{
var movieClip:bear1 = new bear1();
addChild(movieClip);
movieClip.x = 240;
movieClip.y = 45;
movieClip.name = "bear1";
}
function removebear(event:MouseEvent):void
{
removeChild(getChildByName("bear1"));
}
答案 2 :(得分:0)
这完全是一个范围问题 在你的函数addbear中,你创建一个范围为此函数的var,名为movieClip
mix_btn.addEventListener(MouseEvent.CLICK, addbear);
function addbear(event:MouseEvent):void
{
var movieClip:bear1 = new bear1();
addChild(movieClip);
movieClip.x = 240;
movieClip.y = 45;
mix_btn.removeEventListener(MouseEvent.CLICK, addbear);
mix_btn.addEventListener(MouseEvent.CLICK, removebear);
}
function removebear(event:MouseEvent):void
{
var movieClip:bear1 = null;
removeChild(movieClip);
mix_btn.removeEventListener(MouseEvent.CLICK, removebear);
/* mix_btn.addEventListener(MouseEvent.CLICK, addbear);*/
}
要纠正这个问题,你需要将mopvieClip的范围移到函数之外
mix_btn.addEventListener(MouseEvent.CLICK, addbear);
var movieClip:bear1 // **********************global scoping
function addbear(event:MouseEvent):void
{
movieClip = new bear1(); // *************constructing new GLOBAL object here
addChild(movieClip);
movieClip.x = 240;
movieClip.y = 45;
mix_btn.removeEventListener(MouseEvent.CLICK, addbear);
mix_btn.addEventListener(MouseEvent.CLICK, removebear);
}
function removebear(event:MouseEvent):void
{
removeChild(movieClip);//****************** remove movieClip first
movieClip= null; // **********************destroying GLOBAL object here
mix_btn.removeEventListener(MouseEvent.CLICK, removebear);
/* mix_btn.addEventListener(MouseEvent.CLICK, addbear);*/
}