如何在flash as3中将返回函数添加到计时器类中?

时间:2013-02-15 16:12:41

标签: actionscript-3 flash for-loop timer

您好我有关于for循环的问题。 我有一个包含3个MovieClip的数组,我想逐个播放每个动画片段,我计划通过在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;
}

非常感谢你!

2 个答案:

答案 0 :(得分:1)

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

function getNumber():Number {
    return 123;
}

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

请尝试此操作(请参阅注释)。

//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();
    }
}

答案 1 :(得分: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++;
}