0

嗨,我有一个关于 for 循环的问题。我有一个包含 3 个影片剪辑的数组,我想一个接一个地播放每个影片剪辑,我计划通过在 for 循环中使用计时器类来延迟播放();每个 MC。

谁能指出我正确的方向,下面显示的是我使用返回函数的尝试(但是没有成功)

//Shuffling the array to produce a random order when MC's are playing
function randomSort(a:*, b:*):Number
{
    if (Math.random() < 0.5) return -1;
    else return 1;
}
var obstacleArray:Array = [obstacleCar,obstacleCar2,obstacleCar3];
obstacleArray.sort(randomSort);


trace(obstacleArray);
trace(obstacleArray.length);

//OBSTACLE START DELAY
var timerPlay:Timer = new Timer(1000,1);
timerPlay.addEventListener(TimerEvent.TIMER, ontimerPlay);
timerPlay.start();
function ontimerPlay(evt:TimerEvent):void{

    //FOR EACH OBSTACLE

    var _i:Number=-1;
    for(var i:Number=0; i<obstacleArray.length; i++){

        var timerDelay:Timer = new Timer(1000,1);
        timerDelay.addEventListener(TimerEvent.TIMER, ontimerDelay);
        timerDelay.start();
        function ontimerDelay(evt:TimerEvent):void{

            _i = i;
            trace(obstacleArray[i]);
            obstacleArray[i].play();
            trace(i);
        }
    }
    return _i;
}

提前非常感谢您!

4

2 回答 2

1

首先,如果您的函数要返回某些内容,则类型不得为“void”。尔格...

function getNumber():Number {
    return 123;
}

其次,您需要启动计时器,而不是生成创建计时器的函数。另外,我想你会想要跟踪每个 MovieClip 的长度。假设它们可能都不同,那么最简单的方法是在您存储 MovieClips 的同一个数组中。

试试这个(见评论)。

//Shuffling the array to produce a random order when MC's are playing
function randomSort(a:*, b:*):Number {
    if (Math.random() < 0.5) return -1;
    else return 1;
}

// Array holds both the MovieClip reference as well as the duration of the clip.
var obstacleArray:Array = [
    {   "obj":obstacleCar,
        "duration":1050 },
    {   "obj":obstacleCar2,
        "duration":2500 },
    {   "obj":obstacleCar3,
        "duration":3857 }
];

obstacleArray.sort(randomSort);

// Holds the current index we're watching in the array
var currentIndex:int = 0;

// Create the timer and start it up.
var timerPlay:Timer = new Timer(10, 1);
timerPlay.addEventListener(TimerEvent.TIMER, ontimerPlay);
timerPlay.start();

function ontimerPlay(evt:TimerEvent):void {
    obstacleArray[currentIndex].obj.play();

    // Increment the index.
    currentIndex = currentIndex + 1;

    // If the index does not exceed the array length, start a timer for it.
    if (currentIndex < obstacleArray.length) {
        // And use the length specified in the array.
        timerPlay = new Timer(obstacleArray[currentIndex].duration, 1);
        timerPlay.start();
    }
}
于 2013-02-15T16:35:17.893 回答
0

我建议这样做:

// ...

timerPlay.start();

// Move these two variables outside of ontimerPlay. Variables defined in functions 
// generally last only while the function is being executed.
var i:int = 0;
// The timer will fire three times. Each time it fires, i will be incremented.
var timerDelay:Timer = new Timer(1000, 3);

function ontimerPlay(evt:TimerEvent):void{
    // Remove the for loop. The way you had it, three timers were starting and ending 
    // all at the same time.

    timerDelay.addEventListener(TimerEvent.TIMER, ontimerDelay);
    timerDelay.start();
}

function ontimerDelay(evt:TimerEvent):void{
    obstacleArray[i].play();
    i++;
}
于 2013-02-15T16:33:43.223 回答