AS3:如何停止同时播放两个相同的函数?

AS3: how do i stop two of the same function from playing at once?

我正在使用 AS3 创建一个函数,该函数将自动播放一个动画片段,然后将其删除。我的项目将有很多动画过场动画,所以我希望能够调用这个函数,使用过场动画 id 作为参数,然后继续下一个。问题是,我试图连续多次使用该功能来按顺序播放剪辑,但它们都是同时播放的。是否有修复或更好的方法来完全做到这一点?

playClip(new a_walk); //find a way to make these stop playing at the same time 
playClip(new a_door);
//a_walk and a_door are the AS linkage class names for the movieclips im referring to 

function playClip (clip:MovieClip):void {
addChildAt(clip, 0);
clip.mask = myMask;
clip.x=412.4;
clip.y=244.5;

clip.addEventListener(Event.ENTER_FRAME, checkframes);
    function checkframes(event:Event) {
        if (clip.currentFrame == clip.totalFrames) {
            //trace("wow! youre an idiot!"); 
            if (clip.parent) {
                clip.parent.removeChild(clip);
                trace (100);
                return;
            }
        }
    }
}

注意

此答案正在进行中。我正在等待 OP 的回复。

// playClip(new a_door); don't call this yet, or they will just both play. 
var clipData:CustomClass = new CustomClass(); // add an instance of a custom class to hold the value of the movie     
//clip being played (so that you can use this value later in the event handler.)
// it will also hold a value of the next clip
clipData._currentClip = a_walk;
clipData._nextClip = a_door;
playClip(new a_walk); 

function playClip (clip:MovieClip):void {
    addChildAt(clip, 0);
    clip.mask = myMask;
    clip.x=412.4;
    clip.y=244.5;
    clip.addEventListener(Event.ENTER_FRAME, checkframes);
}

function checkframes(event:Event) {
    if (clipData._currentClip.currentFrame == clipData._currentClip.totalFrames) {
        //trace("wow! youre an idiot!"); 
        if (clipData._currentClip.parent) {
            playClip(clipData._nextClip);
            clipData._currentClip.parent.removeChild(clipData._currentClip);
            clipData._currentClip = clipData._nextClip; // moves the clips down
            //clipData._nextClip = here we have 
//a problem. Do these clips play in a set 
//order, first to last? Or do the play out of 
//order jumping back and forth? If so, how 
//are you planning on controlling which clip 
//plays next? 
            trace (100);
            return;
        }
    }
}

我还没有在 Flash 中检查它是否有效,但我注意到您在另一个函数中定义了一个函数,我认为这不是好的做法,所以这可能会为你。试一试并告诉我们。

有机会我会尝试修复上面的代码。与此同时,你回答了我关于按顺序播放剪辑的问题,所以一个简单的解决方案是将所有剪辑放在一个数组中,然后按 playClip(clipArray[i]) 播放它们,然后当剪辑结束并被删除时,做i++ 并调用相同的函数 playClip(clipArray[i]),它将播放数组中的下一个剪辑。

听起来您想要一种机制来播放 MovieClip 队列?如果是这样,您可以通过以下方式完成此操作:

//create an array with the clips you want to play (in order), in my example here, the items can be a MovieClip derived Class, or a MovieClip instance
var playQueue:Array = [a_walk, a_door];

//create a var to store the currently playing clip
var currentClip:MovieClip;

playNext(); //call this when you want the queue of clips to start playing

function playNext():void {
    //if there was an item previously playing (currentClip has a value), stop it and remove it/dispose of it
    if(currentClip){
        currentClip.stop(); //stop it from playing
        currentClip.addFrameScript(currentClip.totalFrames-1, null); //remove the frame script that was added
        currentClip.parent.removeChild(currentClip); //remove it from the display
        currentClip = null;
    }   

    //check if there's anything left to play
    if(playQueue.length < 1) return;

    var nextItem:* = playQueue.shift(); //shift retrieves and removes the first item in the array;

    if(nextItem is Class){
        //if it's a class, instantiate it
        currentClip = new nextItem();
    }else{
        currentClip = MovieClip(nextItem);
    }

    //initialize the movie clip
    addChildAt(currentClip, 0);
    currentClip.gotoAndPlay(1);

    //this is just what you were doing before:
    currentClip.mask = myMask;
    currentClip.x=412.4;
    currentClip.y=244.5;

    //add a command on the last frame of the movie clip to play the next item in the queue
    currentClip.addFrameScript(currentClip.totalFrames-1, playNext);

    //addFrameScript is 0 based, so 0 would refer to the first frame.  This is why we subtract 1 to get the last frame
}

我应该注意,addFrameScript 是一个未记录的函数。它是一个很好的快捷方式,因此您不必让 ENTER_FRAME 侦听器检查 currentFrametotalFrames。然而,由于没有记录,不能指望它在 Flash/AIR 运行时的未来版本中继续存在(尽管它已经存在很长时间了)