2011-12-15 44 views
0

我对游戏中的一些循环功能,这个循环开放的9箱,这里的代码延时与的ActionScript环3

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盒打开,而不是一个一个,我想盒打开包装盒一个接一个,第一个盒子后不久的算法中,我想

open the first box 
delay 5 sec 
open the second box 
delay for 5 sec 

我怎么能做到这一点打开,以便接下来的窗口就会打开,在这里?

回答

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

好吧,有几个选项。

你可以使用的setTimeout延迟一个函数调用:

setTimeout(functionToExecuteAfterDelay, 2000) 

OR

你可以使用Timer类在AS3上设定的时间段执行的功能。

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侦听器。

+0

谢谢@ThomasM,它工作正常,但是当我把它放在循环中似乎不起作用,所有的盒子一次都打开,你有没有想法把锄头放在'for`里? – 2011-12-15 08:54:04

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中你可以使用一个定时器

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; 
} 
+0

感谢兄弟,但箱子有自己的位置,根据舞台宽度。我怎样才能将它们定位为`for`,一些盒子如何为每个其他位置设置不同的位置 – 2011-12-15 08:59:27

+0

无论您使用哪种代码重新定位,请删除箱号上的哪些循环,并使用“nextBoxToOpen”或“ boxToOpen`变量而不是该索引。 – 2011-12-15 09:09:28