2017-04-15 36 views
0

我希望能夠從停止rnd = Math.floor(Math.random()* 3)+ 1;

rnd = Math.floor(Math.random() * 3) + 1; 

得到了不同的號碼,但保持每天產生不同的陳述是真實的時候給了我一個號碼。

if (rnd == 1) { 
     chest1.gotoAndStop("chest"); 
    } 
    if (rnd == 2) { 
     chest2.gotoAndStop("chest"); 
    } 
    if (rnd == 3) { 
     chest3.gotoAndStop("chest"); 
    } 

我希望它確實是這樣一次,然後給了我另一個號碼後,我成功返回的胸部造成另一個胸部出現。但它不是,只是作出了同樣的胸部出現:(

+0

什麼呢'跟蹤(RND);'顯示 – www0z0k

+1

您可能需要使用int變量VAR ** RND: int = Math.random()* 3 + 1; **但除此之外,代碼沒有問題,除非您以某種方式多次運行它。 – Organis

+1

確保您調用「rnd = Math.floor(Math.random()* 3)+ 1;「每次你想要得到一個新的胸部,並且你不只是使用相同的變量值,還要確保你沒有做一些愚蠢的事情,比如使用同一個胸部1,2和3。追蹤你的if語句之前,並在你得到一個隨機數後,看看什麼時候被調用,什麼值是。祝你好運! –

回答

1

這是否工作:

switch (Math.floor(Math.random() * 3) + 1) 
{ 
case 1: 
    chest1.gotoAndStop("chest"); 
    break; 
case 2: 
    chest2.gotoAndStop("chest"); 
    break; 
case 3: 
    chest3.gotoAndStop("chest"); 
    break; 
}