2014-02-07 28 views
1

我確定這個問題已經以百萬種方式被提出過一百萬次,但是無論如何,我都會很感激這個幫助。我正在研究一個Flash Mastermind克隆,並擁有一個包含12個彩色「釘」和一個「洞」圖像的影片剪輯。我將如何編碼一個將選擇五個隨機幀而不僅僅是前五個幀的語句?我有最好的主意,但我不完全確定它是否正確:我該如何編寫一個語句,它將在Flash ActionScript 3中選擇12箇中的5個隨機幀?

var totalColors:Number = 12; 
var maxColors:Number = 5; 
var chosenColors:Array: 

for(var i:Number = 1; i<totalColors; i++) 
{ 
    chosenColors[i] = Math.floor(Math.random()*totalColors)+1 
} 

非常感謝您的幫助!

Movie Clip Colors

請注意,因爲我寫這個的動畫片段被重新排布;我把第一幀 - 「洞」移到了另一層。

編輯:潘先生自從我第一次問這個問題已經幫了很多。爲了測試它,我決定展開我的Bejeweled克隆代碼。首先,我想到了「newPiece.type = Math.ceil(Math.random()* chosenColors.length);」內循環,所以我評論的引用線,這是原代碼的j循環和部分外,與此替代它:

newPiece.type = chosenColors[j]; 

我很抱歉,如果這似乎初級到一些;我不是最強的程序員,尤其是在製作遊戲時。我在ASP.NET和UI設計方面的表現要好得多,但遊戲開發一直吸引着我一些奇怪的,可能是瘋狂的原因。無論如何,這是創建新寶石的方法的一部分。兩個for循環是pan的代碼,用於從十二個中選擇七個隨機幀。不幸的是,電影仍然會從影片剪輯中選取前七個幀。

//i goes through all of the possible colors and adds them to the temp array 
for (var i:uint = 1; i <= newPiece.totalFrames; i++) 
{ 
    temp.push(i); 
} 

//j chooses seven colors out of the array of all possibilities 
for (var j:int = 0; j < numPieces; j++) 
{ 
    //index is the frame that has been chosen randomly 
    var index:int = int(Math.random() * temp.length); 
    chosenColors.push(j); 
    chosenColors[j] = temp[index]; 

    //remove the index 
    temp.splice(index, 0); 
} 

newPiece.type = Math.ceil(Math.random() * chosenColors.length); 

同樣,如果我感到困惑的人與我的惡意代碼到英語的翻譯,這是我的遊戲運行和寶石的圖像影片剪輯,所以你會希望看到我的意思。

Running Game & MovieClip

回答

1
var totalColors:Number = 12; 
var maxColors:Number = 5; 
var chosenColors:Array = []; 
var temp:Array = []; 

for(var i:Number = 1; i <= totalColors; i++) 
{ 
    temp.push(i); 
} 

for (var j:int = 0; j < maxColors; j++) 
{ 

    var index:int = int(Math.random()*temp.length); 
    chosenColors[j] = temp[index]; 

    //remove the index 
    temp.splice(index, 1); 
} 
+0

我已經實現了你的代碼工作寶石迷陣克隆我早些時候發表。我沒有編譯時或運行時錯誤,只要我在第二個for循環中保留這行:但它並不限制可以使用多少種類型的gems。我哪裏做錯了? – Shortstuff81000

+0

你是什麼意思「限制可以使用多少種寶石」? – Pan

相關問題