Be a Supporter!

random tip generator?

  • 362 Views
  • 15 Replies
New Topic Respond to this Topic
BabySteps
BabySteps
  • Member since: Sep. 28, 2011
  • Offline.
Forum Stats
Member
Level 02
Artist
random tip generator? 2012-01-25 13:47:08 Reply

in the menu i want a random tip generator, i've made a movie clip with different movie clips inside that display tips..

but after the animation, i need it to go to a random frame (inside of my movieclip).

does anyone know how i might go about doing this? or what the script is?

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to random tip generator? 2012-01-25 13:50:16 Reply

AS2 or AS3?

Here I'll do AS3:

function generateRandom(num:int):void
{
            tip_mc.gotoAndStop(num);
}
generateRandom(int(Math.random()*tip_mc.totalFrames));
Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to random tip generator? 2012-01-25 14:20:01 Reply

The code that Spysociety posted is incomplete, and as such will not work.
This is because Math.random() returns a variable of type Number that is between 0 and 1. Example being 0.4202162609435618.

You need to use a little math to convert that to a random integer.

The proper way to write Spysociety's code is like so:

function randomInteger(min:int, max:int):int
{
    return Math.floor(Math.random() * (1 + (max - min))) + min;
}

function generateRandom(num:int):void
{
    tip_mc.gotoAndStop(num);
}

generateRandom(randomInteger(1, 10));

The randomNumber() function used in that context will return a psuedo-random integer between the values 1 and 10.

This will only work in AS3.

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Online!
Forum Stats
Supporter
Level 16
Game Developer
Response to random tip generator? 2012-01-25 14:27:15 Reply

His code works fine...maybe you missed the int() that he put in there. Floors the random number * length to get [0,length -1], which works in this language.
:Stupid MATLAB

Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to random tip generator? 2012-01-25 14:42:23 Reply

At 1/25/12 02:27 PM, MSGhero wrote: His code works fine...maybe you missed the int() that he put in there. Floors the random number * length to get [0,length -1], which works in this language.

I didn't miss the int(), I was just not thinking clearly.
I was only thinking in the context of generating a psuedo-random integer of any given range, not specifically an integer with a range always starting at zero.

So I was I wrong about Spysociety's code. It will work in this context.

BabySteps
BabySteps
  • Member since: Sep. 28, 2011
  • Offline.
Forum Stats
Member
Level 02
Artist
Response to random tip generator? 2012-01-25 15:16:01 Reply

SORRY GUYS,

i need it in AS2, forgot to mention that :p

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to random tip generator? 2012-01-25 15:21:46 Reply

The same thing but you change :void to :Void

BabySteps
BabySteps
  • Member since: Sep. 28, 2011
  • Offline.
Forum Stats
Member
Level 02
Artist
Response to random tip generator? 2012-01-25 15:51:41 Reply

At 1/25/12 03:21 PM, Spysociety wrote: The same thing but you change :void to :Void

**Error** Symbol=tip_mc, layer=Layer 2, frame=2:Line 2: The class or interface 'int' could not be loaded.
function generateRandom(num:int):Void

BabySteps
BabySteps
  • Member since: Sep. 28, 2011
  • Offline.
Forum Stats
Member
Level 02
Artist
Response to random tip generator? 2012-01-25 15:53:31 Reply

would anyone mind making an example for me in flash 8 format please?

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to random tip generator? 2012-01-25 15:55:33 Reply

I know this mighty would happen. int does exist in AS2 but maybe could not be used in place of a Number.

Just change num:int to num:Number. This will work then.

Kirk-Cocaine
Kirk-Cocaine
  • Member since: Aug. 17, 2003
  • Offline.
Forum Stats
Moderator
Level 38
Programmer
Response to random tip generator? 2012-01-25 15:56:09 Reply

At 1/25/12 03:51 PM, BabySteps wrote:
At 1/25/12 03:21 PM, Spysociety wrote: The same thing but you change :void to :Void
**Error** Symbol=tip_mc, layer=Layer 2, frame=2:Line 2: The class or interface 'int' could not be loaded.
function generateRandom(num:int):Void

int isn't available in AS2, you have to use Number instead.


The water in Majorca don't taste like what it oughta.

| AS3: Main | AS2: Main | Flash Tutorials |

BBS Signature
Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to random tip generator? 2012-01-25 16:04:50 Reply

At 1/25/12 03:56 PM, Kirk-Cocaine wrote:
At 1/25/12 03:51 PM, BabySteps wrote:
At 1/25/12 03:21 PM, Spysociety wrote: The same thing but you change :void to :Void
**Error** Symbol=tip_mc, layer=Layer 2, frame=2:Line 2: The class or interface 'int' could not be loaded.
function generateRandom(num:int):Void
int isn't available in AS2, you have to use Number instead.

If you write it in AS2:

var num:Number=int(2.1+2.1);
trace(num);

Will output you 4, as it would do in AS3.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to random tip generator? 2012-01-25 16:13:20 Reply

At 1/25/12 04:04 PM, Spysociety wrote: If you write it in AS2:

var num:Number=int(2.1+2.1);
trace(num);

Will output you 4, as it would do in AS3.

I think what he meant was that int is not available as a class.
Such that the following is not valid in AS2:

var foo:Number = int(42); //valid
var bar:int = int(1234); //invalid
BabySteps
BabySteps
  • Member since: Sep. 28, 2011
  • Offline.
Forum Stats
Member
Level 02
Artist
Response to random tip generator? 2012-01-25 16:52:36 Reply

thanks for the help guys, so now it goes to a random frame (like i wanted)
but after it transitions, i would like it to skip to ANOTHER random frame.

http://www.newgrounds.com/dump/item/913e 55afcc30ad6daeaae052045db2fe

help?

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to random tip generator? 2012-01-25 17:11:30 Reply

var skipped:Boolean=false;

function randomize():Void
{
        if(!skipped)
        {
                   generateRandom(Math.random()*tip_mc._totalframes);
                   skipped=true;
        }
        if(transition_mc._currentframe==transition_mc._totalframes)
        {
                    skipped=false;
        }
}

onEnterFrame=randomize;

This may do what you want.

BabySteps
BabySteps
  • Member since: Sep. 28, 2011
  • Offline.
Forum Stats
Member
Level 02
Artist
Response to random tip generator? 2012-01-25 17:41:23 Reply

At 1/25/12 05:11 PM, Spysociety wrote: var skipped:Boolean=false;

function randomize():Void
{
if(!skipped)
{
generateRandom(Math.random()*tip_mc._tot alframes);
skipped=true;
}
if(transition_mc._currentframe==transiti on_mc._totalframes)
{
skipped=false;
}
}

onEnterFrame=randomize;

This may do what you want.

no, that didn't do anything...