2014-07-26 16 views
1

我有一個隨機排序的數組,例如3個項目。我不想在一個動態文本框中顯示所有3個項目(請參閱下面的代碼),而是希望在3個不同的文本框中顯示每個項目。我該如何去做這件事?在多個文本字段中顯示隨機排序的數組

var Questions:Array = new Array; 

Questions[0] = "<b><p>Where Were You Born?</p><br/>"; 
Questions[1] = "<b><p>What is Your Name?</p><br/>"; 
Questions[2] = "<b><p>When is Your Birthday?</p><br/>"; 

function randomize (a:*, b:*): int { 
    return (Math.random() > .5) ? 1: -1; 
} 

questions_txtbox.htmlText = Questions.toString() && Questions.join(""); 
+0

你應該發佈一些代碼,現在不可能給你一個有用的答案。 – Filo

+1

我最深切的歉意。現在包含代碼。 – wingy

回答

1

下面的代碼完成了你所要求的,儘管洗牌功能很粗糙,但它完成了工作。我還動態生成了三個文本字段,而不是在舞臺上創建它們,併爲它們提供了唯一的實例名稱,因此,您需要根據需要調整這些新文本字段的x/y座標。我在Flash CC 2014上測試了它,並且它工作正常。

import flash.text.TextField; 

var Questions:Array = new Array(); 
Questions[0] = "<b><p>Where Were You Born?</p><br/>"; 
Questions[1] = "<b><p>What is Your Name?</p><br/>"; 
Questions[2] = "<b><p>When is Your Birthday?</p><br/>"; 
var shuffleAttempts:int = 10 * Questions.length; 
var questionTextFields:Array = new Array(3); 

function randomize (a:*, b:*): int { 
    return (Math.random() > .5) ? 1: -1; 
} 

function shuffleQuestions(arr:Array):void { 
    var temp:String; 
    for(var i:int = 0; i < shuffleAttempts; i++) { 
     var randIndex1:int = Math.floor(Math.random() * Questions.length); 
     var randIndex2:int = Math.floor(Math.random() * Questions.length); 

     if(randIndex1 != randIndex2) { 
      temp = Questions[randIndex1]; 
      Questions[randIndex1] = Questions[randIndex2]; 
      Questions[randIndex2] = temp; 
     } 

    } 
} 
shuffleQuestions(Questions); // shuffle question list 

for(var questionIndex:int = 0; questionIndex < 3; questionIndex++) { 
    if(questionIndex < Questions.length) { 

     var questionField = new TextField(); // create new text field 
     questionField.htmlText = Questions[questionIndex]; // take a question from the questions list and set the text fields text property 
     questionField.y = questionIndex * 20; // move the text field so that it does not overlap another text field 
     questionField.autoSize = "left"; // autosize the text field to ensure all the text is readable 

     questionTextFields[questionIndex] = questionField; // store reference to question textfield instance in array for later use. 

     addChild(questionField); // add textfield to stage 
    } 
} 
+0

非常感謝!太棒了。 (我從未想過,看起來很簡單的東西需要這麼多的代碼,再次感謝!) – wingy

+0

@wingy,我再次想到了這個昨晚,並意識到'Questions'數組可能需要再次使用,使用它們上的'pop()'方法刪除項目,以便更新該行代碼。 –

+0

謝謝!我非常感謝你的幫助。 – wingy