A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [AS3] Random Frame from Radio Button

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2

    [AS3] Random Frame from Radio Button

    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:

    code:
    if (group.selection == distance) {
    stop();

    submit.addEventListener(MouseEvent.CLICK, submit2Click);
    function submit2Click(event:MouseEvent):void{
    gotoAndPlay(32);
    }
    }



    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:

    code:
    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);
    }
    }
    }
    }



    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.

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Maybe start by organizing and tidying up your codes? Due to a wrong bracket end, the second if statement has been caught up inside the first if statement, and btw, there's something called if/else statement:

    Actionscript Code:
    if(condition){
        // do something if condition is met
    } else if(condition2){
        // do something else if first condition is not met, but this one is
    } else {
        // do something if none of the above conditions are met
    }

    Here's your code, organized and tidied up, making your problem clearly visible:

    Code:
    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);
    			}
    		}
    	}
    }
    Here is the fixed code, just in case you still haven't figured out the error:

    Actionscript Code:
    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);
            }
        } else if (n == 0) {
            submit.addEventListener(MouseEvent.CLICK, submit8Click);
       
            function submit8Click(event:MouseEvent):void{
                gotoAndPlay(501);
            }
        }
    }

    Hope this helps
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thank you for your help, it's working now!

    I had tried to use an else if but I can see now that because the way that my code was structured it was never going to work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center