循环播放淡入和淡出

时间:2012-03-08 17:11:57

标签: actionscript-3 flash

我是as3的新手。我在场景中有一个动画片段。当my_mc淡出时,我想循环播放它必须开始淡入,反之亦然。我想循环播放它们。淡出功能首先工作正常。但是如何在淡出后加载淡入?

my_mc.addEventListener(Event.ENTER_FRAME, fadeout);

function fadeout(e:Event){
    if(my_mc.alpha >=1){
        this.alpha -=0.1;
        if(my_mc.alpha <=0){
            this.removeEventListener(Event.ENTER_FRAME, fadeout);
        }
    }
}

my_mc.addEventListener(Event.ENTER_FRAME, fadein);

function fadein(e:Event){
    if(my_mc.alpha <=0){
        this.alpha +=0.1;
        if(my_mc.alpha >=1){
            this.removeEventListener(Event.ENTER_FRAME, fadein);
        }
    }
}

3 个答案:

答案 0 :(得分:3)

my_mc.addEventListener(Event.ENTER_FRAME, fadeout);

function fadeout(e:Event){
    if(my_mc.alpha <=0){
        my_mc.removeEventListener(Event.ENTER_FRAME, fadeout);
        my_mc.addEventListener(Event.ENTER_FRAME, fadein);
    } else {
        my_mc.alpha -=0.1;
    }
}

function fadein(e:Event){
    if(my_mc.alpha >= 1){
        my_mc.removeEventListener(Event.ENTER_FRAME, fadein);
        my_mc.addEventListener(Event.ENTER_FRAME, fadeout);
    } else {
        my_mc.alpha +=0.1;
    }
}

答案 1 :(得分:1)

fadeout中,一旦检测到alpha为&lt; = 0并删除事件侦听器,只需为fadein函数添加另一个侦听器:

function fadeout(e:Event){
    if(my_mc.alpha >=1){
        this.alpha -=0.1;
        if(my_mc.alpha <=0){
            this.removeEventListener(Event.ENTER_FRAME, fadeout);
            this.addEventListener(Event.ENTER_FRAME, fadein);  // add this
        }
    }
}

如果您希望它反复循环,您可以执行类似于fadein功能的操作。

答案 2 :(得分:0)

我设法改进原来的@sch答案=) 我已经完成了一个函数,你可以将任何给定的movieclip作为参数传递,并将Fadein / fadeout cicle应用于它。这是(评论)的方式:

// A boolean variable holds the flag for this to be applied
var boolFadeinFadeoutCicle:Boolean=true;
// This function can receive as a parameter a movieclip
function fncFadeinFadeout(mc:MovieClip){
    if(boolFadeinFadeoutCicle){
        //The fadeout listener it's added here, all relative to the mc received
            MovieClip(mc).addEventListener(Event.ENTER_FRAME, Fadeout);
        function Fadeout(evt:Event){
            MovieClip(mc).alpha -=0.05;
                    //Once the mc.alpha reaches zero, the fadeout listener it's removed and the fadein one it's added
            if (MovieClip(mc).alpha <= 0){  
                MovieClip(mc).removeEventListener(Event.ENTER_FRAME, Fadeout);
                MovieClip(mc).addEventListener(Event.ENTER_FRAME, Fadein);
            }
                    //Here the logic it's the same, but inverted.
            function Fadein(evt:Event){
                MovieClip(mc).alpha +=0.05;
                if (MovieClip(mc).alpha >= 1){  
                    MovieClip(mc).removeEventListener(Event.ENTER_FRAME, Fadein);
                    MovieClip(mc).addEventListener(Event.ENTER_FRAME, Fadeout);
                }
            }
        }
    }           
}

正如您所看到的,它与@sch答案的逻辑相同,但是具有可以接受MovieClip作为参数的不同结构:

fncFadeinFadeout(mc0);
fncFadeinFadeout(mc0);
fncFadeinFadeout(mc0);
...

所有这些MovieClip都会同样淡出和淡出。 感谢@sch的回答和this tutorial! 我希望你们都发现这种不同的方法很有用;)=)