AS3:如何阻止同时播放两个相同的功能?

时间:2016-08-16 22:17:55

标签: function actionscript-3 flash

我正在使用AS3创建一个功能,它将自动播放动画片段,然后将其删除。我的项目将有很多动画过场动画,所以id喜欢能够调用这个函数,使用过场动画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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

听起来你想要一种机制来播放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运行时的未来版本中继续存在(虽然它已经存在了很长时间)

答案 1 :(得分:0)

注释

这个答案正在进行中。我在等待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]),它将播放数组中的下一个剪辑。