Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-15-2012, 01:32 AM   #1
BlueRocker
Registered User
 
Join Date: Apr 2008
Posts: 11
Default [AS3] Radio Button - help with if/else statement

I have a form that as someone fills it out, animations on the stage become visible. For instance, I have one that says "are you Male or Female" with radio buttons and I want a male of female animation on the stage to become visible when the respective button is selected.

I tried writing my Actionscript 3 code like
ActionScript Code:
MalePerson.visible = false; FemalePerson.visible = false; if(radioMale.selected = true) {         MalePerson.visible = true;     }     else if (radioMale.selected = false){ FemalePerson.visible = true; }

MalePerson is the animation on the stage that will become visible and "radioMake" is that radio buttons instance name. Im just not sure how to write the "if" statement so that the animation will appear when the button is selected and not waiting on a "submit" button to be pressed.

Any help would be appreciated.

Thank you.

Last edited by BlueRocker; 05-15-2012 at 01:59 AM.
BlueRocker is offline   Reply With Quote
Old 05-15-2012, 02:30 AM   #2
USAFDrew2011
Flash_Developer
 
Join Date: Jul 2011
Location: Layton City, Utah
Posts: 192
Default

ActionScript Code:
someButton.addEventListener(MouseEvent.CLICK, checkGender, false, 0, true) function checkGender(event:MouseEvent):void {      MalePerson.visible = false; FemalePerson.visible = false; if(radioMale.selected = true) {         MalePerson.visible = true;     }     else if (radioMale.selected = false){ FemalePerson.visible = true; } }
USAFDrew2011 is offline   Reply With Quote
Old 05-15-2012, 02:41 AM   #3
FusionFactor
Senior Member
 
FusionFactor's Avatar
 
Join Date: Jun 2011
Posts: 181
Default

Quote:
Originally Posted by BlueRocker View Post
Im just not sure how to write the "if" statement so that the animation will appear when the button is selected and not waiting on a "submit" button to be pressed.

Well, you could use a MouseEvent.CLICK listener directly on the radio.

ActionScript Code:
radioMale.addEventListener(MouseEvent.CLICK, checkGender) function checkGender(e:MouseEvent):void {       if(radioMale.selected)       {             MalePerson.visible = true;             FemalePerson.visible = false;       }       else if(!radioMale.selected)       {             MalePerson.visible = false;             FemalePerson.visible = true;       } }

Also, in my opinion, its a bit better practice to use .alpha rather than .visible. Unlike .visible, .alpha is a number (0-1). I just find it easier and more flexible to use.

Last edited by FusionFactor; 05-15-2012 at 02:45 AM.
FusionFactor is offline   Reply With Quote
Old 05-15-2012, 10:42 AM   #4
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,833
Send a message via Skype™ to henke37
Default

Even better, listen for the Event.CHANGE event on the control, it will only fire when there really has been a change.
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 05-15-2012, 12:16 PM   #5
BlueRocker
Registered User
 
Join Date: Apr 2008
Posts: 11
Default

Yes, thank you everyone! That is what I was looking for. I see now I had to turn that into a function and since I did not want it to change based on a button click, the Event.CHANGE worked perfect.
BlueRocker is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:36 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.