Tuesday, 27 January 2015

SHUFFLE TEXT ARRAY AS1 AND AS2


------------------------------------------------------------------------------------------------------------------------
MAKE 7 TEXT BOX WITHOUT INSTANCE NAME
SET TEXT BOX VARIABLES 
_root.ONE , _root.TWO , _root.THREE , _root.FOUR , 
 _root.FIVE  , _root.SIX , _root.SEVEN
-----------------------------------------------------------------------------------------------------------------------
COPY AND PASTE BELOW CODE AS1 OR AS2
READ MORE:
-----------------------------------------------------------------------------------------------------------------------

//MAKE SEVEN TEXT BOXES WITHOUT INSTANCE NAME
//SET TEXT BOXES VARIABLES _root.ONE,_root.TWO,_root.THREE,_root.FOUR,_root.FIVE,_root.SIX,_root.SEVEN
//COPY AND PASTE TEXT IN ACTIONSCRIPT PANEL

TextArray = new Array ("ONE;TWO;THREE;FOUR;FIVE;SIX;SEVEN");
TextArray[0] = TextArray[0].split(";");
Text = ShuffleArrayFuntion(TextArray[0].slice(0));
_root.ONE   =   Text[0];
_root.TWO   =   Text[1];
_root.THREE =   Text[2];
_root.FOUR  =   Text[3];
_root.FIVE  =   Text[4];
_root.SIX   =   Text[5];
_root.SEVEN =   Text[6];

function ShuffleArrayFuntion (TextArray) {
ShuffleArray = new Array();
do{
Text  = int(Math.random()*TextArray.length);
ShuffleArray.push(TextArray[Text]);
TextArray.splice(Text, 1);
}
while (TextArray.length>0);
return (ShuffleArray);
}
----------------------------------------------------------------------------------------------------------------
OTHER CODES
https://www.youtube.com/watch?v=9heKW6PO4gs
https://sites.google.com/site/2013readingnotejiansenlu/chapter-september-2013/chapter-9-5-question-and-answer-flash

Randomizing questions and answers

-----------------------------------------------------------------------------------------------------------------
//http://board.flashkit.com/board/showthread.php?319889-Randomizing-questions-and-answers
//Randomizing questions and answers


function display() {
gotoAndPlay("gameover");
} else {
answers = shuffleArray(zArray[questionNumber][1].slice(0));
question.text = zArray[questionNumber][0];
answer0.text = answers[0];
answer1.text = answers[1];
answer2.text = answers[2];
answer3.text = answers[3];
questionNumDisplay = questionNum+1+" of "+zArray.length;
correctAnswer = zArray[questionNumber][1][0];
animateIn();
}
}
function shuffleArray(array1) {
array2 = new Array();
do {
r = int(Math.random()*array1.length);
array2.push(array1[r]);
array1.splice(r,1);
} while (array1.length > 0);
return(array2);
}
------------------------------------------------------------------------------------------------------------------
//declare your question and answer arrays
question = new Array("question1", "question2", "question3");
answer = new Array("answer1", "answer2", "answer3");

//set up a function to call whenever a new question is requested
nextElement = function(){
Element = random(question.length);
questionField = this.question[Element];
finalAnswer = this.answer[Element];
question.splice(Element, 1);
answer.splice(Element, 1);
trace(question);
trace(answer);
}

nextElement();

-------------------------------------------------------------------------------------------------------------
//http://stackoverflow.com/questions/11980657/as3-random-array-randomize-array-actionscript-3

var arr : Array = [0,1,2,3,4,5,6,7,8,9];

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

trace( arr.sort( randomize ) );
--------------------------------------------------------------------------------------------------------------------
OR USE THIS CODE FOR TEXT ARRAY IN AS2
https://www.kirupa.com/forum/showthread.php?261781-random-sort-with-Arrays-in-AS-3-0
---------------------------------------------------------------------------------------------------------------------

var testArray:Array = new Array("a", "b", "c", "d", "e");
trace (shuffleArray(testArray));
txt1.text= shuffleArray(testArray)
function shuffleArray(arr:Array):Array {
var len:Number = arr.length;
var temp =0;
var i:Number = len;
while (i--) {
var rand:Number = Math.floor(Math.random() * len);
temp = arr[i];
arr[i] = arr[rand];
arr[rand] = temp;
}
return arr;
}



EmoticonEmoticon