Be a Supporter!

Randomness in actionscript?

  • 207 Views
  • 5 Replies
New Topic Respond to this Topic
sonic180
sonic180
  • Member since: Feb. 4, 2006
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Randomness in actionscript? 2010-12-20 21:08:06 Reply

I want to make this little thing where, lets say I click a button and it selects a random phrase that I have typed out and tells the player that.

Example:
Phrases (You're horrible at this. You're great at this)
And then when you click the button then it chooses one of those phrases and says that.

Does anyone know how to do this? I was thinking maybe arrays, but I am not sure how to use those... Any help would be appreciated

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to Randomness in actionscript? 2010-12-20 21:22:58 Reply

Just have an array holding all your string, for when you press your button just do it randomize and pick the sorted value from the array

Example:

var array:Array = new Array("blah", "bleh", "blih");
var integer:int = Math.random()*3;
trace(ar[id]);

It will random pick one of the values you have in your array, hope this helps :)

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to Randomness in actionscript? 2010-12-20 21:24:52 Reply

Oh sorry:

[...]
trace(ar[integer]);

Hope this helps :)

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to Randomness in actionscript? 2010-12-20 21:28:00 Reply

ARGH!
trace(ar[integer]);
// sorry, I wrote this code for one of my projects and just changed the names to you understand, sorry -.- \\
Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to Randomness in actionscript? 2010-12-20 21:29:42 Reply

WHAT IS HAPPENING? I DID THE MISTAKE AGAIN!! ARGH!
trace(array[integer]);
=O WHAT IS HAPPENING? LOL
visua
visua
  • Member since: Dec. 21, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Randomness in actionscript? 2010-12-21 23:30:54 Reply

Here is a nice function you could save into a class package for use later on:

private function randRange(minNum:Number, maxNum:Number):Number 
{
    return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}

You could use it for instance like this;

enemyThing = randRange(0, myArray.length);

BBS Signature