make selection buttons using loop
I have 4 questions in one screen. 2 questions have Yes/No as choices, 2 questions have Yes/No/NA as choices. Each button has 2 states: regular, rollover; I use location control to display the right state correctly based on user choice. I can hard code each button fine, but would like to include rollover, rollout, and release scripts in a loop.
With someone's help, I finally got the question numbers right, be able to pick up the instance name (as strings) correctly, but it can't display the right image for each action.
It's feel it's so close but also so far away from getting it right. Please help. Thanks.
The source file is at: http://dl.dropbox.com/u/30106423/buttons.zip
totalQuestions = 4;
ButtonsForYes();
ButtonsForNo();
ButtonsForNA();
C1_1_sel = 1;// for testing only: to have answers selected.
C1_2_sel = 2;
C1_3_sel = 2;
C1_4_sel = 3;
showAnswers();
// show current answers
function showAnswers():Void {
for (var i:Number = 1; i<=totalQuestions; i++) {
if (this["C1_"+i+"_sel"] == 1) {
this["C1_"+i+"_1_RO"]._x = this["C1_"+i+"_1"]._x+3;
this["C1_"+i+"_1_RO"]._y = this["C1_"+i+"_1"]._y+2;
} else if (this["C1_"+i+"_sel"] == 2) {
this["C1_"+i+"_2_RO"]._x = this["C1_"+i+"_2"]._x+3;
this["C1_"+i+"_2_RO"]._y = this["C1_"+i+"_2"]._y+2;
} else if (this["C1_"+i+"_sel"] == 3) {
this["C1_"+i+"_3_RO"]._x = this["C1_"+i+"_3"]._x+3;
this["C1_"+i+"_3_RO"]._y = this["C1_"+i+"_3"]._y+2;
}
}
}
function resetAll():Void {
if (selectedQuestion == 1) {
C1_1_sel = 0;
C1_1_1_RO._x = -1000;
C1_1_1_RO._y = -1000;
C1_1_2_RO._x = -1000;
C1_1_2_RO._y = -1000;
} else if (selectedQuestion == 2) {
C1_2_sel = 0;
C1_2_1_RO._x = -1000;
C1_2_1_RO._y = -1000;
C1_2_2_RO._x = -1000;
C1_2_2_RO._y = -1000;
} else if (selectedQuestion == 3) {
C1_3_sel = 0;
C1_3_1_RO._x = -1000;
C1_3_1_RO._y = -1000;
C1_3_2_RO._x = -1000;
C1_3_2_RO._y = -1000;
C1_3_3_RO._x = -1000;
C1_3_3_RO._y = -1000;
} else if (selectedQuestion == 4) {
C1_4_sel = 0;
C1_4_1_RO._x = -1000;
C1_4_1_RO._y = -1000;
C1_4_2_RO._x = -1000;
C1_4_2_RO._y = -1000;
C1_4_3_RO._x = -1000;
C1_4_3_RO._y = -1000;
}
}
/* NOTE:
MC instance names:
regular state
q1- yes: C1_1_1, no: C1_1_2
q2- yes: C1_2_1, no: C1_2_2
q3- yes: C1_3_1, no: C1_3_2, NA: C1_3_3
q4- yes: C1_4_1, no: C1_4_2, NA: C1_4_3
rollover state
add _RO after the names for regular states
question answer variables:
q1 - C1_1_sel yes = 1, no = 2
q2 - C1_2_sel yes = 1, no = 2
q3 - C1_3_sel yes = 1, no = 2, NA = 3
q4 - C1_4_sel yes = 1, no = 2, NA = 3
*/
function ButtonsForYes():Void {
for (var i:Number = 1; i<=totalQuestions; i++) {
var btn:MovieClip = this["C1_"+i+"_1"];
this["C1_"+i+"_1"].Num = i;
btn.onRollOver = function():Void {
trace("***** RO starts *****");
trace("rollover YES on question "+this.Num);
selectedQuestion = this.Num;
this["C1_"+selectedQuestion+"_1_RO"]._x = this["C1_"+selectedQuestion+"_1"]._x+3;
this["C1_"+selectedQuestion+"_1_RO"]._y = this["C1_"+selectedQuestion+"_1"]._y+2;
trace(["ro image instance: "+"C1_"+selectedQuestion+"_1_RO"]);
trace("x loc: "+["C1_"+selectedQuestion+"_1_RO"]._x);
trace("***** RO ends *****");
};
btn.onRollOut = function():Void {
trace("rollout YES on question "+this.Num);
selectedQuestion = this.Num;
if (this["C1_"+selectedQuestion+"_sel"] != 1) {
this["C1_"+selectedQuestion+"_1_RO"]._x = -1000;
this["C1_"+selectedQuestion+"_1_RO"]._y = -1000;
} else {
this["C1_"+selectedQuestion+"_1_RO"]._x = this["C1_"+selectedQuestion+"_1"]._x+3;
this["C1_"+selectedQuestion+"_1_RO"]._y = this["C1_"+selectedQuestion+"_1"]._y+2;
}
};
btn.onRelease = function():Void {
trace("***** Press starts *****");
trace("selected YES on question "+this.Num);
selectedQuestion = this.Num;
whichQVar = ["C1_"+this.Num+"_sel"];
resetAll();
//this["C1_resetAll"+i+"()"];// call function to reset locations
this["C1_"+selectedQuestion+"_1_RO"]._x = this["C1_"+selectedQuestion+"_1"]._x+3;
this["C1_"+selectedQuestion+"_1_RO"]._y = this["C1_"+selectedQuestion+"_1"]._y+2;
this[whichQVar] = 1;
trace("question variable name to set: "+whichQVar);
trace("answers q 1 to 4: "+C1_1_sel+" "+C1_2_sel+" "+C1_3_sel+" "+C1_4_sel);
trace("***** Rress ends *****");
};
}
}
function ButtonsForNo():Void {
for (var i:Number = 1; i<=totalQuestions; i++) {
var btn:MovieClip = this["C1_"+i+"_2"];
this["C1_"+i+"_2"].Num = i;
btn.onRollOver = function():Void {
trace("***** RO starts *****");
trace("rollover No on question "+this.Num);
selectedQuestion = this.Num;
this["C1_"+selectedQuestion+"_2_RO"]._x = this["C1_"+selectedQuestion+"_2"]._x+3;
this["C1_"+selectedQuestion+"_2_RO"]._y = this["C1_"+selectedQuestion+"_2"]._y+2;
trace(["ro image instance: "+"C1_"+selectedQuestion+"_2_RO"]);
trace("x loc: "+["C1_"+selectedQuestion+"_2_RO"]._x);
trace("***** RO ends *****");
};
btn.onRollOut = function():Void {
trace("rollout No on question "+this.Num);
selectedQuestion = this.Num;
if (this["C1_"+selectedQuestion+"_sel"] != 2) {
this["C1_"+selectedQuestion+"_2_RO"]._x = -1000;
this["C1_"+selectedQuestion+"_2_RO"]._y = -1000;
} else {
this["C1_"+selectedQuestion+"_2_RO"]._x = this["C1_"+selectedQuestion+"_2"]._x+3;
this["C1_"+selectedQuestion+"_2_RO"]._y = this["C1_"+selectedQuestion+"_2"]._y+2;
}
};
btn.onRelease = function():Void {
trace("***** Press starts *****");
trace("selected No on question "+this.Num);
selectedQuestion = this.Num;
whichQVar = ["C1_"+this.Num+"_sel"];
resetAll();
this["C1_"+selectedQuestion+"_2_RO"]._x = this["C1_"+selectedQuestion+"_2"]._x+3;
this["C1_"+selectedQuestion+"_2_RO"]._y = this["C1_"+selectedQuestion+"_2"]._y+2;
this[whichQVar] = 2;
trace("question variable name to set: "+whichQVar);
trace("answers q 1 to 4: "+C1_1_sel+" "+C1_2_sel+" "+C1_3_sel+" "+C1_4_sel);
trace("***** Rress ends *****");
};
}
}
