Hello and welcome to our community! Is this your first visit?
Register
Results 1 to 6 of 6
  1. #1
    Devoted Veteran Darren's Avatar

    Join Date
    Aug 2005
    Location
    Ireland
    Posts
    12,260

    Actionscript Help - Random Function?

    So I was given this assignment for college to create an 8-ball interface with a "try me" button. Any time you click the button, the 8-ball should flash up one of eight answers, you know the kind of thing, "looks doubtful", "outlook is bleak" etc. I know all about event handling/listeners etc from java, so I can at least get the try me button to work lol. All I need to know is, is there a function in actionscript similar to java's Math.random class? The more detailed the answer the better, thanks in advance.
    [center:3qmfvtqh][/center:3qmfvtqh]

  2. #2
    Veteran Enthusiast
    Join Date
    May 2006
    Location
    Midlands, UK
    Posts
    5,638

    Re: Actionscript Help - Random Function?

    Is the 8 ball a MC? Are all the responses on the same Scene timeline? And are all the responses on separate frames?

    If it's all on the same scene, and each response is a different frame, then
    Code:
    on (release){
    what_number = Number(random(10))+1;
    gotoAndStop(_root.what_number);
    }
    Will work just fine. Just put that into your button and your ready to go, the only flaw is a 1/9 chance of the AS directing you to the start frame, but that would simply appear as if the user hadn't clicked properly. Also, with the current code, your last response has twice the probablity of coming up, if this is an issue, change the
    Code:
    (10))+1;
    bit of the code to
    Code:
    (8))+1;

  3. #3
    Veteran Enthusiast LeadingManNigel's Avatar


    Join Date
    May 2005
    Location
    New York City
    Posts
    6,213

    Re: Actionscript Help - Random Function?


    Create Array and input values, each value is separated by a comma.
    Code:
     
    var myArray:Array = new Array("Eat Pie,"Buy me a sammich","Darkdemon.org")
    //If you trace your array
    trace(myArray)
    // You should get Eat Pie, Buy me a sammich, Darkdemon.org
    Arrays have a bunch of different functions like
    myArray.push
    myArray.splice
    and so on.(Just check out the AS3 documentation for all of them.)
    If you want to get a random value from an array you can do this:

    To pull random values from an array
    Code:
    var myArray:Array = new Array("You Will Die",
    							  "Your Day will be good",
    							  "You like penuts",
    							  "Penuts will eat your soul")
    // Create Array, I just put them in a list like this
    // so its easier to read
    stage.addEventListener(MouseEvent.CLICK, returnArrayValue)
    function returnArrayValue(e:MouseEvent)
    {
    	var i:int = Math.floor(Math.random()* myArray.length)
    	// Will return a random value between 0 and the length of your array
    	// Its floored so it wont go beyond your array limits
    	trace(myArray[i])
    	// Returns a random value from your array
    	// to your output panel
    }
    If your gonna OOP this dont forget to import the right stuff, I know I do xD

  4. #4
    Devoted Veteran Darren's Avatar

    Join Date
    Aug 2005
    Location
    Ireland
    Posts
    12,260

    Re: Actionscript Help - Random Function?

    Thanks guys, I ended up robbing someone else in the class for his code lol. I'm on an assigment ten times harder now, a guessing game with a bunch of shit in it. It's difficult considering I have absolutely no actionscript besides gotoandplay
    [center:3qmfvtqh][/center:3qmfvtqh]

  5. #5
    Veteran Enthusiast ULTRA1337piv0tz's Avatar

    Join Date
    Mar 2008
    Location
    The Netherlands
    Posts
    6,737

    Re: Actionscript Help - Random Function?

    You're lucky though, there are so many tutorials on AS hanging around the webs.
    Skype: ruben_vanwijk
    Steam: xendlessnl

  6. #6
    Devoted Veteran Darren's Avatar

    Join Date
    Aug 2005
    Location
    Ireland
    Posts
    12,260

    Re: Actionscript Help - Random Function?

    All hail niall, master and commander of the universe. This can be locked now if the staff feel so inclined c:
    [center:3qmfvtqh][/center:3qmfvtqh]


 

Posting Permissions

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