dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Random Number Issues

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1

    Random Number Issues

    I'm trying to code a frame where a random number is generated (one to two for example), and then if the number one is generated, it jumps to a certain frame, if two is generated, it jumps to a certain frame. But for some reason the code keeps re-generating a new random number and I keep getting errors.

    Code:
    trace("Firing Attack Generation - Frame Two");
    stop();
    var attackchoice:int = randomNumber(1, 2);
    
    /* Generate dat number! */
    
    function randomNumber(low:Number=0, high:Number=1):Number
    {
      return Math.floor(Math.random() * (1+high-low)) + low;
    }
    
      if (attackchoice == 1)
    {
    	trace("Attack Choice One");
    	gotoAndPlay(3);
    }
    if (attackchoice == 2)
    {
    	trace("Attack Choice Two");
    	gotoAndPlay(61);
    }
    else
    {
    	trace(attackchoice)
    	trace("THIS ISNT SUPPOSED TO HAPPEN");
    }
    Any help (or a fix to what I should change/rewrite) would be best.

    Thanks.

    Here is a snippet of my code: (Please change the .com part, had to change it) mediafire.c0m/?vaszdzimnwc3h1r
    Last edited by mutank; 09-10-2011 at 10:24 AM.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    When you hit the frame with that code - it's going to run through all of it every time you land there. You need a little check on that variable to make sure you only set it on the first playthrough.

    PHP Code:
    trace("Firing Attack Generation - Frame Two");
    stop();

    var 
    attackchoice:int;

    //  if the var is 0 - it's our first time at this frame...if not, we don't need to re-roll the choice
    if(attackchoice == 0attackchoice randomNumber(12);

    (
    attackchoice 2) ? gotoAndPlay(3) : gotoAndPlay(61);



    function 
    randomNumber(low:Number=0high:Number=1):Number{
      return 
    Math.floor(Math.random() * (1+high-low)) + low;

    Please use [php] or [code] tags, and mark your threads resolved 8)

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