Skip navigation
01matthewst
Currently Being Moderated

Random Frame from Radio Button

Apr 13, 2012 8:48 AM

Tags: #a #flash #cs5.5 #button #action #action_script_3 #actionscript3 #if #actionscript_3.0 #random_frame #random_number

Hi Guys,

I'm a complete Flash newbie, all I've done is a few days of tutorials and stuff on the Internet. I'm knocking together a prototype for a University project.

I have a radio button list, and once a button is selected and submit is pressed it takes the user to a certain frame. Each radio button represents a different condition, and for the distance condition I want it to take the user to one of two frames; one which shows the animation close up, and one which shows it far away.

Before I attempted to have this button selection send the user to one of two frames, I had it working just sending them to the one. This is the code that I used:

[as]if (group.selection == distance) {
  stop();
 
  submit.addEventListener(MouseEvent.CLICK, submit2Click);
  function submit2Click(event:MouseEvent):void{
  gotoAndPlay(32);
}
}[/as]

 

From what I've found on the Internet I believe that I need to generate random numbers and have this within an if statement. I've played about with various combinations with no luck. Below is what I currently have:

 

[as]if (group.selection == distance) {
  stop();
 
  var n:Number = Math.round(Math.random()* 1+0);
  trace(n);
 
  if (n == 1) {
                  submit.addEventListener(MouseEvent.CLICK, submit2Click);
  function submit2Click(event:MouseEvent):void{
  gotoAndPlay(32);
  }
 
  if (n == 0) {
  submit.addEventListener(MouseEvent.CLICK, submit8Click);
  function submit8Click(event:MouseEvent):void{
  gotoAndPlay(501);
  }
  }
}
}[/as]

 

The trace shows me that the number generating is working, but when I click submit it always take me to frame 32 no matter what the number is. I'm unsure as to whether an if within an if is something which is going to work? I've been trying to get this to work for a couple of hours now and have run out of ideas. If any of you guys could give me some help then that would be great.

Thank you, Tom.

 
Replies
  • Currently Being Moderated
    Apr 13, 2012 10:22 AM   in reply to 01matthewst

    Your radio buttons should only be needed to assign a value to a variable.  That variable would represent the frame number that you intend to go to.

     

    You should only have one event listener for your submit button and it should only have one event handler function.  It should not be inside any conditional code.  The event handler function only needs to use the value that the radio button assigned to the variable.

     

      var selectedFrame:uint;   // assign this value with your radio buttons

     

      submit.addEventListener(MouseEvent.CLICK, submitClick); // only need this one listener

     

      function submitClick(event:MouseEvent):void{ // only need this one function

           gotoAndPlay(selectedFrame);

      }

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2012 7:43 PM   in reply to 01matthewst

    Your code for interacting with the radio buttons will be where you assign a value to that variable.  If you don't yet have any code for dealing with the selection of your radios, then you need that before you do anything with them.

     

    What you will end up doing with that code is assigning a value to that variable based on which radio was selected.  The radio button has a "value' property.  You will be assign one to each of your radios.  These 'value's will be the frame numbers that your radio is suppose to lead to.

     

    selectedFrame = your selected radio button's value property... don't code this, just try to understand it

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points