3 Replies Latest reply: Apr 13, 2012 7:43 PM by Ned Murphy RSS

    Random Frame from Radio Button

    01matthewst

      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.

        • 1. Re: Random Frame from Radio Button
          Ned Murphy CommunityMVP

          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);

            }

          • 2. Re: Random Frame from Radio Button
            01matthewst Community Member

            Hi Ned,

             

            Thank you for your reply.

             

            You'll have to excuse my lack of knowledge, but I'm a little confused around the var selectedFrame:uint; part. How do I go about assigning this value with my radio buttons?

             

            I thought based on what I've seen in tutorials that it would be something like:

             

            var selectedFrame:uint;

            ideal.selectedFrame = 6;

             

            Where 'ideal' is the instance name of one of my radio buttons, and I'd do the same for each button, but this is producing the following error:

             

            1119: Access of possibly undefined property selectedFrame through a reference with static type fl.controls:RadioButton.

             

            Kind Regards, Tom.

            • 3. Re: Random Frame from Radio Button
              Ned Murphy CommunityMVP

              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