Skip to main content
sergey_landar
Inspiring
January 15, 2010
Question

Why RadioButton still selected?

  • January 15, 2010
  • 2 replies
  • 519 views

When i start game again my radioButton selected. But in startProgram function i told : .selected=false;

Where the problem?

Attached .fla.

code:

import fl.controls.RadioButtonGroup;

var radioGroup1:RadioButtonGroup=new RadioButtonGroup("Coin Loss");
eagle_rb.group=radioGroup1;
tails_rb.group=radioGroup1;

var randomNumber:Number;
var points:Number=0;

function startProgram():void {
     eagle_rb.visible=true;
     tails_rb.visible=true;
     coin_mc.visible=true;
     eagle_rb.selected=false;
     tails_rb.selected=false;
     
     playAgain_btn.label="Play Again";
     coinToss_btn.label="Coin Toss";
     coinToss_btn.useHandCursor=true;
     playAgain_btn.useHandCursor=true;
     points_txt.text="0";
     eagle_rb.label="Eagle";
     tails_rb.label="Tails";
     error_txt.text="";

     playAgain_btn.visible=false;

     coinToss_btn.addEventListener(MouseEvent.CLICK, checkAnswer);

     playAgain_btn.visible=true;
     playAgain_btn.enabled=false;

     coinToss_btn.enabled=true;

     points=0;


}
startProgram();

function checkAnswer(event:MouseEvent):void {
     randomNumber=Math.random();
     trace(randomNumber);
     if (randomNumber<0.5) {
          coin_mc.gotoAndStop("eagle");
     } else {
          coin_mc.gotoAndStop("tails");
     }

     if (radioGroup1.selection==null) {
          error_txt.text="Need choose any answer!";
          return;
     } else if (radioGroup1.selection.label=="Eagle" && randomNumber<0.5) {
          error_txt.text=radioGroup1.selection.label+" - correct";
          points+=100;
          points_txt.text=String(points);
     } else if (radioGroup1.selection.label=="Eagle" && randomNumber>0.5) {
          error_txt.text=radioGroup1.selection.label+" - wrong";
          points-=100;
          points_txt.text=String(points);
     } else if (radioGroup1.selection.label=="Tails" && randomNumber>0.5) {
          error_txt.text=radioGroup1.selection.label+" - correct";
          points+=100;
          points_txt.text=String(points);
          coin_mc.gotoAndStop("tails");
     } else {
          error_txt.text=radioGroup1.selection.label+" - wrong";
          points-=100;
          points_txt.text=String(points);
     }


     if (points==500||points==-500) {
          gameResult();
     }
}

function gameResult():void {
     playAgain_btn.enabled=true;

     if (points==500) {
          message_txt.text="You are winner! You win 500 points!";
          coinToss_btn.enabled=false;
     } else if (points==-500) {
          message_txt.text="You are lost! You must pay 500 points;)";
          coinToss_btn.enabled=false;
     }

     playAgain_btn.addEventListener(MouseEvent.CLICK, playAgain);

     error_txt.text="";
     eagle_rb.visible=false;
     tails_rb.visible=false;
     coin_mc.visible=false;
}

function playAgain(event:MouseEvent):void {
     startProgram();

}

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
January 16, 2010

As Dave mentioned, there is no provision for deselecting a radio button using code... only by selecting another.

Another option, if you are resetting things, is to just use the timeline to reset things.  This could be done by having all your code and content in frame 2, and your reset button executes gotoAndPlay(1), thereby resetting the entire stage to initial conditions.

sergey_landar
Inspiring
January 16, 2010

Yes, i see..

Thank you !

By the way as i clear need make first radiobutton always true, yes? (for example like for quiz...)

And for my example maybe better use checkboxes?

January 15, 2010

If you check the Help you will find this:

selected property - You can only set this value to true; setting it to false has no effect. To achieve the desired effect, select a different radio button in the same radio button group.

You can fix by creating a temporary button on the fly, not adding it to the stage, and then selecting it... Try adding these three lines to the end of your

startProgram() function:

var tempR:RadioButton = new RadioButton()
tempR.group = radioGroup1;
tempR.selected = true;