我有一些游戏的循环功能,这个循环用于打开9框,这里是代码
function random_item_2(coinsx)
{
var listItem:Array = new Array();
for (var i:uint=0; i<15; i++)
{
listItem.push(i);
}
ItemLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var thisItem:FirstBox = new FirstBox();
thisItem.x = x * IcardHorizontalSpacing + IboardOffsetX;
thisItem.y = y * IcardVerticalSpacing + IboardOffsetY;
var r:int = Math.floor(Math.random() * listItem.length);
thisItem.cardface = listItem[r];
listItem.splice(r,1);
thisItem.gotoAndStop(thisItem.cardface+2);
var itemFound = this.foundItem(thisItem.cardface);
if (itemFound == 50 || itemFound == 100 || itemFound == 250 || itemFound == 500 || itemFound == 1000)
{
var itemC = Number(coinsx) + Number(itemFound);
coinsx = itemC;
update_coins(Number(coinsx));
info_coinstext(String(coinsx));
trace('Gold Coins Found > '+itemFound);
}else if(itemFound!='Kosong'){
updateItem(itemFound);
trace('Item Found > '+itemFound);
}
addChild(thisItem);
ItemLeft++;
}
}
}
问题是,一次打开9个盒子,而不是一个接一个,我想打开盒子一个接一个,打开第一个盒子后打开下一个盒子,这里有一些我想要的算法< / p>
open the first box
delay 5 sec
open the second box
delay for 5 sec
我怎么能这样做?
答案 0 :(得分:2)
嗯,有几种选择。
您可以使用setTimeout来延迟函数调用:
setTimeout(functionToExecuteAfterDelay, 2000)
OR
您可以使用as3中的Timer类在设定的时间段内执行函数。
var myTimer:Timer = new Timer(5000,9); //Will tick 9 times, each after 5000 milliseconds
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();
function someFunction(event:TimerEvent) {
//do your openingstuff here
}
就个人而言,我会选择选项2.此外,在完成后删除TimerEvent监听器。
答案 1 :(得分:1)
要在for循环中添加延迟,您应该写下这样的内容:
function random_item_2(coinsx)
{
var listItem:Array = new Array();
for (var i:uint=0; i<15; i++)
{
listItem.push(i);
}
ItemLeft = 0;
const DELAY:int = 5000;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
setTimeout(addItem, (x*boardHeight+y)*DELAY, x, y);
}
}
}
function addItem(x:uint, y:uint) : void
{
var thisItem:FirstBox = new FirstBox();
thisItem.x = x * IcardHorizontalSpacing + IboardOffsetX;
thisItem.y = y * IcardVerticalSpacing + IboardOffsetY;
var r:int = Math.floor(Math.random() * listItem.length);
thisItem.cardface = listItem[r];
listItem.splice(r,1);
thisItem.gotoAndStop(thisItem.cardface+2);
var itemFound = this.foundItem(thisItem.cardface);
if(itemFound == 50 || itemFound == 100 || itemFound == 250 || itemFound == 500 || itemFound == 1000)
{
var itemC = Number(coinsx) + Number(itemFound);
coinsx = itemC;
update_coins(Number(coinsx));
info_coinstext(String(coinsx));
trace('Gold Coins Found > '+itemFound);
}
else if(itemFound!='Kosong')
{
updateItem(itemFound);
trace('Item Found > '+itemFound);
}
addChild(thisItem);
ItemLeft++;
}
每次设置超时时增加延迟,稍后将调用该函数。
答案 2 :(得分:0)
这是setInterval
var interval:int=0;
var nextBoxToOpen:int=0;
var maxBoxes:int = 9;
function openBox(e:TimerEvent):void {
//code to open box;
nextBoxToOpen++;
if(nextBoxToOpen >= maxBoxes) {
nextBoxToOpen=0;
clearInterval(interval);
}
}
function startOpen():void {
interval = setInterval(openBox, 5000);
}
OR,在AS3中你可以使用一个Timer
var tmr:Timer = new Timer(5000, 9); //Interval = 5000 ms, Number of ticks = 9
tmr.addEventListener(TimerEvent.TIMER, openBox);
tmr.start();
function openBox():void {
var boxToOpen = tmr.currentCount;
//code to open box;
}