Reputation: 43
please help me with a little project. I have 9 different dinamic text with 9 default value. What i need! When i press 'enter' must random chose and display only one single value. like a roulete or slot machine. initial value must be on the same place but only one to be display.
On second press keyboard (enter) it must change and display initial value. and loop of that
right now i made a code to change value on value1.text (chose random from array)
function randomJob(){
var jobs:Array = new Array("apple", "lemon", "banana", "orange", "mandarin", "lime", "kiwi", "pear", "apricot");
value1.text = jobs[randomNumber(jobs.length-1)];
}
function randomNumber(max){
return(Math.round(Math.random()*max));
}
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler(event : KeyboardEvent) : void
{
if (event.keyCode == Keyboard.ENTER)
{
randomJob();
}
}
example:
Upvotes: 0
Views: 119
Reputation: 9839
You have just to know if it's your first "enter" to select a text field, or the second one to reset text fields texts, so you can do like this :
var fruits:Array = ['apple', 'lemon', 'banana', 'orange', 'mandarin', 'lime', 'kiwi', 'pear', 'apricot'];
var texts:Array = []; // array to put your text fields
var first_time:Boolean = true; // indicates if it's the first enter
for(var i:int = 0; i < fruits.length; i++){
var text_field:TextField = new TextField();
text_field.x = 50;
text_field.y = 50 + 28*i;
text_field.height = 25;
text_field.text = fruits[i];
text_field.border = true;
addChild(text_field);
texts.push(text_field);
}
stage.addEventListener(
KeyboardEvent.KEY_DOWN,
function (e:KeyboardEvent):void {
if (e.keyCode == Keyboard.ENTER){
if(first_time){
first_time = false;
var index:int = Math.round(Math.random()*(fruits.length-1));
for(var i:int = 0; i<fruits.length; i++){
if(i != index){
// if it's not the randomly selected text field, then set its text to "bad luck"
texts[i].text = 'bad luck';
}
}
} else {
// if you want repeat again, set first_time = true
//first_time = true;
for(i = 0; i < fruits.length; i++){
// reset all text fields
texts[i].text = fruits[i];
}
}
}
}
)
Hope that can help.
Upvotes: 1