For循环包含一个事件监听器函数。但是循环迭代比监听器函数更快。在监听器功能完成之前,下一次迭代开始。怎么办呢???
答案 0 :(得分:1)
听起来你不需要“for”循环。
根据你的描述,看起来你需要某种形式的事件链,在第一个事件完成后你设置一个新的监听器等等......
答案 1 :(得分:1)
如果您正在寻找的答案如PatrickS所述,我已经创建了一个如何实现它的示例:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite
{
private var _queue:Queue;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var queueItems:Vector.<QueueItem> = new Vector.<QueueItem>();
for (var i:uint = 0; i < 5; i++)
{
var timer:Timer = new Timer(2 * 1000, 1);
queueItems.push(new QueueItem(timer, TimerEvent.TIMER_COMPLETE, onTimerComplete));
}// end for
_queue = new Queue(queueItems);
var currentTimer:Timer = Timer(_queue.currentQueueItem.eventDispatcher);
currentTimer.start();
_queue.shift();
}// end function
private function onTimerComplete(e:TimerEvent):void
{
if (_queue.currentQueueItem)
{
var currentTimer:Timer = Timer(_queue.currentQueueItem.eventDispatcher);
currentTimer.start();
if (_queue.length > 0) _queue.shift();
}// end if
}// end function
}// end class
}// end package
import flash.events.IEventDispatcher;
import flash.events.Event;
internal class Queue
{
private var _queueItems:Vector.<QueueItem>;
public function get currentQueueItem():QueueItem { return (_queueItems.length > 0) ? _queueItems[0] : null }
public function get length():int { return _queueItems.length }
public function Queue(queueItems:Vector.<QueueItem>)
{
_queueItems = queueItems;
}// end function
public function shift():void
{
var eventDispatcher:IEventDispatcher = _queueItems[0].eventDispatcher;
var type:String = _queueItems[0].type;
var listener:Function = _queueItems[0].listener;
var useCapture:Boolean = _queueItems[0].useCapture;
var priority:int = _queueItems[0].priority;
var useWeakReference:Boolean = _queueItems[0].useWeakReference;
eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
_queueItems.shift();
}// end function
}// end class
internal class QueueItem
{
private var _eventDispatcher:IEventDispatcher;
private var _type:String;
private var _listener:Function;
private var _useCapture:Boolean;
private var _priority:int;
private var _useWeakReference:Boolean;
public function get eventDispatcher():IEventDispatcher { return _eventDispatcher }
public function get type():String { return _type}
public function get listener():Function { return _listener }
public function get useCapture():Boolean { return _useCapture }
public function get priority():int { return _priority }
public function get useWeakReference():Boolean { return _useWeakReference }
public function QueueItem(eventDispatcher:IEventDispatcher, type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false)
{
_eventDispatcher = eventDispatcher;
_type = type;
_listener = listener;
_useCapture = useCapture;
_priority = priority;
_useWeakReference = useWeakReference;
}// end function
}// end class
简单地说,这会创建一个类似于系统的队列,它会将事件侦听器添加到队列前面的IEventListener
对象中,并在添加队列时进行移位。
以上显示了如何在Timer
个对象中使用它。