PDA

View Full Version : [AS3] Storing Radio Button Selections


james_martin
01-05-2010, 10:09 PM
Hello

Newbie Alert!!

I am having some problems with a small survey swf i am making in actionscript 3. I have multiple choice questions on different frames using radio buttons and i need the users selections shown in a dynamic text button along each frame and also on the last frame of the survey.

Another problem i am getting is i need the last radio button clicked to be stored, so if the user clicks lets say 'rb_2' on frame 1, clicks next to go to frame 2, then if they were to go back to frame 1 again 'rb_2' is still selected.

Thought it would be easy using the 'Shared Object Class' but from reading tutorials i cannot get it working. Is this the best way or should i be looking into another solution?

Cheers

James

scorpion9
01-05-2010, 10:17 PM
as far as i know variables defined in MainTimeline are global....accessible in every frame.
so if u have defined a variable rb_2 on first frame then its value should exist on all other frames too. but i might be wrong. i used it long time ago.

LOLFlash
01-05-2010, 11:04 PM
Make your check boxes along full movie and set them visible=true/false

james_martin
01-06-2010, 10:03 AM
Sorry i should have posted some of my code really!! I left it out originally as it just wasn't working but i have got parts of it working now.

Thanks for the replies guys, before seeing your replies i kept trying to use the 'shared Object' class to solve this...

I have got the users selection shown on each frame in dynamic text boxes and the radio button on frame one is stored so if you click 'rb_3' on frame 1, click next for frame 2 then go back again to frame 1 again 'rb_3' will still be selected. Problem is this only works for frame 1, the stored data for frame 2 is now saving.

If anyone can help get this code working? I am sure that the problem lies in calling '(so.data["value"] ==".....") on both frame 1 and 2 or am i wrong?

the full code i am using is :

Frame 1>
// Import Radio Button Controls.
import fl.controls.RadioButtonGroup;

// Set Variable of Rb Group.
var rbGroup:RadioButtonGroup = new RadioButtonGroup( "myRBGroup" );

// Set Variable of Shared Object.
var so:SharedObject = SharedObject.getLocal("options");

// Assign Group and Values to Rb's.
rb_1.group=rbGroup;
rb_1.value = "macbook";
rb_2.group=rbGroup;
rb_2.value = "macbook pro";
rb_3.group=rbGroup;
rb_3.value = "imac";
rb_4.group=rbGroup;
rb_4.value = "emac";
rb_5.group=rbGroup;
rb_5.value = "mac pro";
rb_6.group=rbGroup;
rb_6.value = "mac mini";

// Add Event Listeners on Rb's.
rb_1.addEventListener(MouseEvent.CLICK, showResult);
rb_2.addEventListener(MouseEvent.CLICK, showResult);
rb_3.addEventListener(MouseEvent.CLICK, showResult);
rb_4.addEventListener(MouseEvent.CLICK, showResult);
rb_5.addEventListener(MouseEvent.CLICK, showResult);
rb_6.addEventListener(MouseEvent.CLICK, showResult);

//Define function on Rb's.
function showResult(event:MouseEvent):void {
switch (rbGroup.selection) {
case rb_1 :
stage_1_txt.text = "macbook" ;
break;
case rb_2 :
stage_1_txt.text="macbook pro";
break;
case rb_3 :
stage_1_txt.text="imac";
break;
case rb_4 :
stage_1_txt.text="emac";
break;
case rb_5 :
stage_1_txt.text="mac pro";
break;
case rb_6 :
stage_1_txt.text="mac mini";
break;
}
}

// Frame Navigation.
stop();

next_btn.addEventListener(MouseEvent.MOUSE_UP,nxt) ;

// Define and Trace Rb Choice on Button.
// Flush so to retrieve in later use.
function nxt(e:MouseEvent):void {
gotoAndStop(2);
trace( rbGroup.selection.value );
so.data["value"] = rbGroup.selection.value;
so.data["value"] = rbGroup2.selection.value;
so.flush();
}

// Define Radio Button according to last Click.
if ( so.data["value"] == "macbook" ) {
rbGroup.selection = rb_1;
stage_1_txt.text="macbook";
}
if ( so.data["value"] == "macbook pro" ) {
rbGroup.selection = rb_2;
stage_1_txt.text="macbook pro";
}
if ( so.data["value"] == "imac" ) {
rbGroup.selection = rb_3;
stage_1_txt.text="imac";
}
if ( so.data["value"] == "emac" ) {
rbGroup.selection = rb_4;
stage_1_txt.text="emac";
}
if ( so.data["value"] == "mac pro" ) {
rbGroup.selection = rb_5;
stage_1_txt.text="mac pro";
}
if ( so.data["value"] == "mac mini" ) {
rbGroup.selection = rb_6;
stage_1_txt.text="mac mini";
}

Second Frame >
// Set Variable of Rb Group.
var rbGroup2:RadioButtonGroup=new RadioButtonGroup("Group 2");

// Assign Group and Values to Rb's.
rb_7.group=rbGroup2;
rb_7.value = "one month";
rb_8.group=rbGroup2;
rb_8.value = "six months";
rb_9.group=rbGroup2;
rb_9.value = "one year";
rb_10.group=rbGroup2;
rb_10.value = "two years";
rb_11.group=rbGroup2;
rb_11.value = "three years";
rb_12.group=rbGroup2;
rb_12.value = "four years";

// Add Event Listeners on Rb's.
rb_7.addEventListener(MouseEvent.CLICK, showResult2);
rb_8.addEventListener(MouseEvent.CLICK, showResult2);
rb_9.addEventListener(MouseEvent.CLICK, showResult2);
rb_10.addEventListener(MouseEvent.CLICK, showResult2);
rb_11.addEventListener(MouseEvent.CLICK, showResult2);
rb_12.addEventListener(MouseEvent.CLICK, showResult2);

//Define function on Rb's.
function showResult2(event:MouseEvent):void {
switch (rbGroup2.selection) {
case rb_7 :
stage_2_txt.text="one month";
break;
case rb_8 :
stage_2_txt.text="six months";
break;
case rb_9 :
stage_2_txt.text="one year";
break;
case rb_10 :
stage_2_txt.text="two years";
break;
case rb_11 :
stage_2_txt.text="three years";
break;
case rb_12 :
stage_2_txt.text="four years";
break;
}
}

// Frame Navigation.
stop();

next2_btn.addEventListener(MouseEvent.MOUSE_UP,nxt 2);
back_btn.addEventListener(MouseEvent.MOUSE_UP,back );

// Define and Trace Rb Choice on Button.
function nxt2(e:MouseEvent):void{
gotoAndStop(3);
trace( rbGroup2.selection.value );
so.data["value"] = rbGroup2.selection.value;
}
// Define and Trace Rb Choice on Button.
function back(e:MouseEvent):void{
gotoAndStop(1);
trace( rbGroup2.selection.value );
so.data["value"] = rbGroup.selection.value;
}

// Define Radio Button according to last Click.
if ( so.data["value"] == "one month" ) {
rbGroup2.selection = rb_7;
stage_2_txt.text="one month";
}
if ( so.data["value"] == "six months" ) {
rbGroup2.selection = rb_8;
stage_2_txt.text="six months";
}
if ( so.data["value"] == "one year" ) {
rbGroup2.selection = rb_9;
stage_2_txt.text="one year";
}
if ( so.data["value2"] == "two years" ) {
rbGroup2.selection = rb_10;
stage_2_txt.text="two years";
}
if ( so.data["value"] == "three years" ) {
rbGroup2.selection = rb_11;
stage_2_txt.text="three years";
}
if ( so.data["value"] == "four years" ) {
rbGroup2.selection = rb_12;
stage_2_txt.text="four years";
}

If anyone can help point me in the right direction to get this code working? If not i will try other options but i am trying to get this working as it is so close!

Cheers

LOLFlash
01-06-2010, 06:39 PM
first frame:

var init:Boolean;
if(!init){
var answersAr:Array=new Array(questionsLegth+1);
this.addEventListener(MouseEvent.CLICK,screenCLICK );
init=true;
}

function screenCLICK(evt:MouseEvent){

var rb = evt.target as RadioButton;
if(!rb) return;
answersAr[this.currentFrame]=rb.label;

}

james_martin
01-06-2010, 09:52 PM
LolFlash, thanks for the reply, i actually have this now working,

Dawsonk on Flashkit forums helped me out, solution can be seen here:

http://board.flashkit.com/board/showthread.php?t=807867

In the same way as you i changed to using arrays and it worked.

Cheers