需要事件监听器才有效

时间:2014-07-30 02:21:21

标签: flash

我需要一些关于AS的更多技术帮助。我希望不同的电影剪辑以随机顺序在舞台上播放并永远重复。我需要在代码中添加一个EventListener,它看到影片剪辑结束并开始播放下一个影片剪辑。我已经使用过计时器,但这并不好,因为我想观看整个影片剪辑,所以忽略代码中的计时器。你能告诉我一个解决方案吗?

import flash.display.MovieClip;



var clip1:clip01 = new clip01  ;

var clip2:clip02 = new clip02  ;

var clip3:clip03 = new clip03  ;

var clip4:clip04 = new clip04  ;

var clip5:clip05 = new clip05  ;



var files:Array = new Array();

pushArray(clip1,clip2,clip3,clip4,clip5);



function pushArray(c1,c2,c3,c4,c5:MovieClip){

                files.push(c1);

                files.push(c2);

                files.push(c3);

                files.push(c4);

                files.push(c5);

                }



function randomizeArray(array:Array):Array

{

                var newArray:Array = new Array();

                while (array.length > 0)

                {

                                newArray.push(array.splice(Math.floor

                                (Math.random()*array.length), 1)[0]);

                }

                return newArray;

}



var RandomArray:Array = randomizeArray(files);



var testTimer:Timer = new Timer(1000);

    testTimer.addEventListener(TimerEvent.TIMER,updateFile);
    testTimer.start();



function updateFile(event:TimerEvent):void

{

                if (RandomArray.length == 0)

                {

                                pushArray(clip1,clip2,clip3,clip4,clip5);

                                RandomArray = randomizeArray(files);

                }

                trace('play file',RandomArray[0]);

                RandomArray.shift();



}



//addChild(RandomArray[0]);

1 个答案:

答案 0 :(得分:0)

这是一个解决方案:

var c1:C1 = new C1();
var c2:C2 = new C2();
var c3:C3 = new C3();
var c4:C4 = new C4();
var c5:C5 = new C5();

var files:Array = [c1, c2, c3, c4, c5];
var n:uint = files.length;
var c:MovieClip;
var run:Boolean = false;
var r:uint;
var oldr:uint;

this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
function enterFrameHandler(e:Event):void {
    if (!run) {
        if(c) this.removeChild(c);
        r = rand(n);
        r = (r == oldr) ? (r + 1)%n : r; // not the same twice
        c = MovieClip(files[r]);
        this.addChild(c);
        oldr = r;
    }
    run = (c.currentFrame == c.totalFrames) ? false : true;
}

function rand(n:uint):uint {
    return Math.floor(Math.random() * n);
}