on (press) { var i:int = Math.randomRange(1,30) if ( i ==1){ // my request is when i = 1 , the var which named (question ) load question number 1 from settings.txt and when i = 2 ,question number 2 and so on question = question1 this.loadVariables("settings.txt"); }else if ( i ==2){ question = question2 this.loadVariables("settings.txt"); }else if ( i ==3){ question = question3 this.loadVariables("settings.txt"); } }
settings.txt is:
PHP Code:
&question1= put here any question & &question2= put here any question & &question3= put here any question &
var question = variables["question" + String(i)];
trace(question);
myTextField.text = String(question);
}
Code:
question1= put here any question 1
&question2= put here any question 2
&question3= put here any question 3
&question4= put here any question 4
&question5= put here any question 5
&question6= put here any question 6
&question7= put here any question 7
&question8= put here any question 8
&question9= put here any question 9
&question10= put here any question 10
&question11= put here any question 11
&question12= put here any question 12
&question13= put here any question 13
&question14= put here any question 14
&question15= put here any question 15
&question16= put here any question 16
&question17= put here any question 17
&question18= put here any question 18
&question19= put here any question 19
&question20= put here any question 20
&question21= put here any question 21
&question22= put here any question 22
&question23= put here any question 23
&question24= put here any question 23
&question25= put here any question 25
&question26= put here any question 26
&question27= put here any question 27
&question28= put here any question 28
&question29= put here any question 29
&question30= put here any question 30
I didn't think it would work with swishmax as it is AS3, I don't use swishmax, but it would give you a better way of getting the correct random line for you question.
Looking at your original code, there are several issues to look at. First of all, the text you're loading.
Code:
&question1= put here any question &
&question2= put here any question &
&question3= put here any question &
This should be this instead:
Code:
question1=put here any question1&question2=put here any question2&question3=put here any question3
The & symbol is for separating each section into different variables. The only reason I would do it would be to make it more readable. It's still wasteful. Still, having the extra space after the = sign will show up.
Code:
on (press) {
var i:int = Math.randomRange(1,30)
if ( i ==1){
// my request is when i = 1 , the var which named (question ) load question number 1 from settings.txt and when i = 2 ,question number 2 and so on
question = question1
this.loadVariables("settings.txt");
}else if ( i ==2){
question = question2
this.loadVariables("settings.txt");
}else if ( i ==3){
question = question3
this.loadVariables("settings.txt");
}
}
This is messy in many ways. First of all, the loadVariables function loads all the vars into the object you target. Because of this you should only run it once in this case.
Code:
on (press) {
this.loadVariables("settings.txt");
var i:int = Math.randomRange(1,30)
if ( i ==1){
// my request is when i = 1 , the var which named (question ) load question number 1 from settings.txt and when i = 2 ,question number 2 and so on
question = question1
}else if ( i ==2){
question = question2
}else if ( i ==3){
question = question3
}
}
The next part is loading external data take a bit. So you need to set up an interval. This is basically a function that keeps activating a target function over the time you set. For example
Code:
var param_interval = setInterval(checkParamsLoaded, 100);
This creates a variable to store the interval. It will call the function checkParamsLoaded every 100 milliseconds.
After moving things around, this is what I came up with. First I divided the code into two different areas. One is on the button and the other is on the base timeline. For the button, this is my code:
Code:
on (press) {
this.loadVariables("settings.txt");
_root.param_interval = setInterval(_root.tFun, 100);
}
On the root timeline this is the rest of the code:
Code:
function randRange(min:Number, max:Number):Number {
return Math.floor(Math.random()*(max-min+1))+min;
}
function tFun() {
trace("-=-=-");
trace("-"+btn["question"+randRange(1,3)]+"-");
if (btn.done == "done") {
clearInterval(param_interval);
}
}
var param_interval;
I also added another variable to be loaded from the settings.txt file: "&done=done" This is good for indicating that the file has been loaded.