2016-05-08 9 views
0

私はコロナSDKを使用しており、ランダムな色を表示したいと考えています。これは私のコードですが、私の色は常に同じです。何故ですか?同じ画像をmath.randomで表示していますか?

local boxColors = { 
     "BoxColors/Gold.png", 
     "BoxColors/Blue.png", 
     "BoxColors/Green.png", 
     "BoxColors/Orange.png", 
     "BoxColors/Purple.png", 
     "BoxColors/Rose.png", 
     "BoxColors/Yellow.png" 
    } 

groundColor =display.newImage(boxColors[math.random(#boxColors)],0,0) 

おかげで、ここでは種類reagrds

+0

をそれが動作しますか?そうでなければおそらくmath.random thatsが壊れていない – dannash918

+0

https://forums.coronalabs.com/topic/301-display-a-random-image-every-shake/ - あなたがしようとしているものと非常に似ています。 –

答えて

0

あなたのコードはどうですか、私はここで試してみましたが、私のために働きました。新しい画像をインスタンス化する前にgroundColor:removeSelf()を追加する必要があります。コロナでは上書きされません。

あなただけの色を変更する場合は、このような何か試してください:あなたはdisplay.newImage(boxColors [1]、0,0)を使用する場合

local centerX = display.contentCenterX 
local centerY = display.contentCenterY 

local colors = { 
    { 1,1,0 }, -- yellow 
    { 0,0,1 }, -- blue 
    { 0,1,0 }, -- green 
    { 1,0,0 }, -- red 
} 

rect = display.newRect(centerX, centerY, 100, 100) 
rect.fill = colors[math.random(#colors)] 

function onTouch(event) 
    if event.phase == "began" then 
     rect.fill = colors[math.random(#colors)] 
    end 
end 
rect:addEventListener("touch", onTouch) 
0

は、私はとっくの昔作られた小ぎれいなシャッフル機能です。 math.randomのランダム性はちょっとでもかまいませんので、以下の関数は少し冒険的です。

func shuffleArray(myArray) 

    for i=0,30 do -- Do the below 30 times 
    for temp = 1, #myArray do 
     n = math.random(1, #myArray) -- Select a random index from myArray and assign it to n. 
     temp1 = myArray[temp] -- Take the loop number(temp) as an index from myArray and assign it to temp1. 
     myArray[temp] = myArray[n] -- Replace the temp index taken from above with the randomly selected SoundList index. 
     myArray[n] = temp1 -- Replace the randomly chosen index with the temp index. 
    end 
    end 

return myArray -- Return the shuffled array. 
end 

関数を呼び出して配列を渡すだけで、いつでも好きなように編集することができます。

種類よろしく、

Krivvenz。

関連する問題