为什么我的动画在继续之前不等待事件侦听器?

发布于 2025-01-08 14:10:25 字数 1079 浏览 0 评论 0原文

    var playGraph = new Chart();
var playPart1 = new Part1();
var playPart2 = new Part2();
var playPart3 = new Part3(); 

addChild(playGraph);
playGraph.gotoAndPlay(1);
var s1:SoundOne = new SoundOne();
s1.play();

playGraph.addEventListener(Event.COMPLETE, onCompleteGraph); 
playPart1.addEventListener(Event.COMPLETE, onCompletePart1); 
playPart2.addEventListener(Event.COMPLETE, onCompletePart2); 

function onCompleteGraph(evt:Event):void
{
    playPart1.x = 370;
    playPart1.y = 190;
    addChild(playPart1);
    playPart1.gotoAndPlay(1);
}


function onCompletePart1(evt:Event):void
{

    playPart2.x = 100;
    playPart2.y = 100;
    addChild(playPart2);
    playPart2.gotoAndPlay(1);
    var s2:Sound2 = new Sound2();
    s2.play();

}

function onCompletePart2(evt:Event):void
{
    removeChild(playPart2);
    addChild(playPart3);
    playPart3.gotoAndPlay(1);
    var s3:Sound3 = new Sound3();
    s3.play();

}

我的问题是,我不想删除Child(playPart1);。我想让孩子留下来。但是 - 如果我不删除它,一切都会正常进行。如果我确实删除它,它似乎忽略了在进入 playPart1 之前必须完成 playGraph 的事实。知道为什么会这样吗?

    var playGraph = new Chart();
var playPart1 = new Part1();
var playPart2 = new Part2();
var playPart3 = new Part3(); 

addChild(playGraph);
playGraph.gotoAndPlay(1);
var s1:SoundOne = new SoundOne();
s1.play();

playGraph.addEventListener(Event.COMPLETE, onCompleteGraph); 
playPart1.addEventListener(Event.COMPLETE, onCompletePart1); 
playPart2.addEventListener(Event.COMPLETE, onCompletePart2); 

function onCompleteGraph(evt:Event):void
{
    playPart1.x = 370;
    playPart1.y = 190;
    addChild(playPart1);
    playPart1.gotoAndPlay(1);
}


function onCompletePart1(evt:Event):void
{

    playPart2.x = 100;
    playPart2.y = 100;
    addChild(playPart2);
    playPart2.gotoAndPlay(1);
    var s2:Sound2 = new Sound2();
    s2.play();

}

function onCompletePart2(evt:Event):void
{
    removeChild(playPart2);
    addChild(playPart3);
    playPart3.gotoAndPlay(1);
    var s3:Sound3 = new Sound3();
    s3.play();

}

My question is, I don't want to removeChild(playPart1);. I want the child to stay. However - if I don't removeChild it, everything plays just fine. If I do remove it, it seems as if it disregards the fact that it has to finish playGraph before moving onto playPart1. Any idea why this would be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2025-01-15 14:10:25

我假设 Chart/Part1/Part2 是 MovieClip 符号,它们在到达最后一帧时调度 COMPLETE 事件?如果这是正确的,那么我可以对正在发生的事情做出合理的猜测。

您假设在将符号添加到舞台并调用 gotoAndPlay 之前,它们不会播放。这是不正确的,一旦您实例化一个符号,它就会开始在后台播放,也就是说,前 4 行代码将创建符号并开始播放它们,即使它们在屏幕上不可见,因此,您的 COMPLETE 事件将在您预期之前在后台触发。

解决方案是在实例化这些符号后立即停止播放,然后在触发前一个 COMPLETE 事件后开始播放。

var playGraph = new Chart();
var playPart1 = new Part1();
var playPart2 = new Part2();
var playPart3 = new Part3(); 

//Stop the symbols from playing automatically
playPart1.stop();
playPart2.stop();
playPart3.stop();

addChild(playGraph);
playGraph.gotoAndPlay(1);
var s1:SoundOne = new SoundOne();
s1.play();

playGraph.addEventListener(Event.COMPLETE, onCompleteGraph); 
playPart1.addEventListener(Event.COMPLETE, onCompletePart1); 
playPart2.addEventListener(Event.COMPLETE, onCompletePart2); 

function onCompleteGraph(evt:Event):void
{
    playPart1.x = 370;
    playPart1.y = 190;
    addChild(playPart1);
    playPart1.gotoAndPlay(1);
}


function onCompletePart1(evt:Event):void
{

    playPart2.x = 100;
    playPart2.y = 100;
    addChild(playPart2);
    playPart2.gotoAndPlay(1);
    var s2:Sound2 = new Sound2();
    s2.play();

}

function onCompletePart2(evt:Event):void
{
    removeChild(playPart2);
    addChild(playPart3);
    playPart3.gotoAndPlay(1);
    var s3:Sound3 = new Sound3();
    s3.play();

}

I'm assuming Chart/Part1/Part2 are MovieClip symbols that dispatch the COMPLETE event when they reach the last frame? If that's correct, then I can take a reasonably good guess at what's happening.

You're making the assumption that your symbols will not play until you add them to the stage and call gotoAndPlay. This is incorrect, as soon as you instantiate a symbol it will begin playing in the background, that is, the first 4 lines of code will both create the symbol and begin playing them even though they're not visibly on the screen, as such, your COMPLETE events will fire in the background before you expect.

The solution is to stop these symbols from playing as soon as you instantiate them, and then begin playing once the previous COMPLETE event has fired.

var playGraph = new Chart();
var playPart1 = new Part1();
var playPart2 = new Part2();
var playPart3 = new Part3(); 

//Stop the symbols from playing automatically
playPart1.stop();
playPart2.stop();
playPart3.stop();

addChild(playGraph);
playGraph.gotoAndPlay(1);
var s1:SoundOne = new SoundOne();
s1.play();

playGraph.addEventListener(Event.COMPLETE, onCompleteGraph); 
playPart1.addEventListener(Event.COMPLETE, onCompletePart1); 
playPart2.addEventListener(Event.COMPLETE, onCompletePart2); 

function onCompleteGraph(evt:Event):void
{
    playPart1.x = 370;
    playPart1.y = 190;
    addChild(playPart1);
    playPart1.gotoAndPlay(1);
}


function onCompletePart1(evt:Event):void
{

    playPart2.x = 100;
    playPart2.y = 100;
    addChild(playPart2);
    playPart2.gotoAndPlay(1);
    var s2:Sound2 = new Sound2();
    s2.play();

}

function onCompletePart2(evt:Event):void
{
    removeChild(playPart2);
    addChild(playPart3);
    playPart3.gotoAndPlay(1);
    var s3:Sound3 = new Sound3();
    s3.play();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文