00:00
00:00
Newgrounds Background Image Theme

switizuwu just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Random text in a movie clip. (AS3)

1,473 Views | 20 Replies
New Topic Respond to this Topic

I'm making a preloader and every time someone enters the frame, I want it to display a different piece of text - a little funny quote.

I've set it up so that each quote is placed in a key frame. I want the actionscript to pick a random frame, and thus a different piece of text pops up.

I've fiddled about with actionscript, but currently, it only pulls up the first three frames, with an uncanny preference for the first frame. Is there a more suitable method for doing this.

Thanks

Response to Random text in a movie clip. (AS3) 2013-02-12 18:10:42


Math.random() generates a pseudo-random number between 0 and 1. Multiply it by the number of frames in total minus 1, and add 1 at the end to get a random number between 1 and the number of total frames.

Math.random() * (totalFrames - 1) + 1;

Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Random text in a movie clip. (AS3) 2013-02-12 18:11:18


The code I found was:

function getRandomLabel():String {
var labels:Array = new Array("rt1", "rt2", "rt3", "rt4");
var index:Number = Math.floor(Math.random() * labels.length);
return labels[index];
}
this.gotoAndStop(getRandomLabel());

I label frames 1 to 4 "rt1" to "rt4", but it just doesn't work properly.

Interestingly, on testing it in flash, I may sometimes get it to display the 4th frame, but never when I run the .swf file from my desktop.

Response to Random text in a movie clip. (AS3) 2013-02-12 18:21:51


use code tags when posting code. It preserves formatting and allows us to read it a bit easier.

i'll assume you know very little ActionScript and have no real interest in programming, so i'll just edit your function for you.

gotoAndStop(Math.round(Math.random() * (totalFrames - 1) + 1));

replace all of what you posted with that.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Random text in a movie clip. (AS3) 2013-02-12 18:22:19


At 2/12/13 06:10 PM, egg82 wrote: Math.random() generates a pseudo-random number between 0 and 1. Multiply it by the number of frames in total minus 1, and add 1 at the end to get a random number between 1 and the number of total frames.

Math.random() * (totalFrames - 1) + 1;

That code works. Kind of.

Unfortunately, it still won't pull up that fourth frame.

Response to Random text in a movie clip. (AS3) 2013-02-12 18:26:02


At 2/12/13 06:22 PM, edhe wrote:
At 2/12/13 06:10 PM, egg82 wrote: Math.random() generates a pseudo-random number between 0 and 1. Multiply it by the number of frames in total minus 1, and add 1 at the end to get a random number between 1 and the number of total frames.

Math.random() * (totalFrames - 1) + 1;
That code works. Kind of.

Unfortunately, it still won't pull up that fourth frame.

You can use a function that only will pick numbers min <= x <= max.

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

So for example, if you want to get a random frame between 1 and 4, you can do:

var randFrame:Number = getRandomNumberRange(1, 4);
mc.gotoAndStop(randFrame);

Hope that helps.

Response to Random text in a movie clip. (AS3) 2013-02-12 18:27:16


At 2/12/13 06:22 PM, edhe wrote: That code works. Kind of.

Unfortunately, it still won't pull up that fourth frame.
trace(Math.round(Math.random() * (4 - 1) + 1));

output:

3
2
3
1
3
1
3
1
3
2
2
3
1
2
1
2
3
3
4
2
3
2
3
2
3
2
4
1
1
2
3
2
3
3
3
2
4
3
1
3
1
1
2
1
3
1
3
3
1
3
2
3
1
2
3
3
2
1
2
1
2
2
1
4
1
4
1
2
1
1
2
4
1
3
1
3
3
2
2
3
1
4
4
2
3
1
2
2
2
3
2
2
3
2
2
1
3
2
2
2
1
4
2
3
2
1
1
1
3
1
4
2
2
4
1
2
3
1
1
1
1
3
2
2
4
2
3
1
2
3
2
2
1
4
3
3
3
1
2
3
4
3
3
2
4
2
3
2
3
2
2
2
3
4
3
3
3
2
2
3
1
4
3
3
2
3
4
3
4
4
2
3
2
3
3

Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Random text in a movie clip. (AS3) 2013-02-12 18:41:04


also, due to the nature of randomness, the first and last values are always going to have a lower chance of appearing than other values.

proof of concept:

private var _numbers:Vector.<uint> = new Vector.<uint>;

private function onEnterFrame(e:Event):void {
	_numbers[Math.round(Math.random() * 9)]++;
}

output:

(0) : 276
(1) : 554
(2) : 535
(3) : 518
(4) : 548
(5) : 590
(6) : 544
(7) : 579
(8) : 557
(9) : 299

5000 iterations.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Random text in a movie clip. (AS3) 2013-02-12 18:43:53


At 2/12/13 06:27 PM, egg82 wrote:
At 2/12/13 06:22 PM, edhe wrote: That code works. Kind of.

Unfortunately, it still won't pull up that fourth frame.
trace(Math.round(Math.random() * (4 - 1) + 1));

output:

3...

When I'm opening my .swf file, I'm Ctrl+LEFT, CTRL+RIGHT tapping at such a rate that there is no doubt that the fourt frame isn't appearing. I must have refreshed the frame at least 50 times, but it isn't coming up.

@Spysociety I tried your Actionscript, and again, it worked fine, but I can't for the life of me get it to show any frames past #3. I tried applying the actionscript inside the movie clip, and I tried applying it to the movieclip, but to no avail.

Response to Random text in a movie clip. (AS3) 2013-02-12 18:53:26


At 2/12/13 06:43 PM, edhe wrote: When I'm opening my .swf file, I'm Ctrl+LEFT, CTRL+RIGHT tapping at such a rate that there is no doubt that the fourt frame isn't appearing. I must have refreshed the frame at least 50 times, but it isn't coming up.

you don't test random numbers like that, silly!

@Spysociety I tried your Actionscript, and again, it worked fine, but I can't for the life of me get it to show any frames past #3. I tried applying the actionscript inside the movie clip, and I tried applying it to the movieclip, but to no avail.

trace out totalFrames from the MovieClip you're trying to access.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Random text in a movie clip. (AS3) 2013-02-12 19:05:37


At 2/12/13 06:53 PM, egg82 wrote: trace out totalFrames from the MovieClip you're trying to access.

I'd have no idea how to. Maybe I'm a lost cause.

I might have to rethink this whole idea. It's just not something I'm able to pick up.

My original point stands though. When using any of these pieces of AS3, anything past frame 3 *never* shows up on my screen. Except when I'm publishing the movie and watching it within Flash.

In fact, I've just discovered something. The actionscript works perfectly well... within Flash. When I publish it and I'm watching it in the window that pops up, it does what I want it to.

But when I open up the .swf on the desktop, it just refuses to work.

Response to Random text in a movie clip. (AS3) 2013-02-12 19:08:25


OH TITS! I'VE BEEN OPENING THE WRONG FILE!!!

Instead of "FTLW Preloader v1 REVISE", I've been opening "FTLW Preloader" - the old version.

I've been opening the wrong one for a whole bloody hour.

Do carry on, and thanks for the help, both of you.

Response to Random text in a movie clip. (AS3) 2013-02-13 00:58:56


At 2/12/13 06:41 PM, egg82 wrote: also, due to the nature of randomness, the first and last values are always going to have a lower chance of appearing than other values.

this is totally off topic, but i didn't know that... But now a problem i had in the past makes total sense lol


If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.

Response to Random text in a movie clip. (AS3) 2013-02-13 09:48:53


At 2/12/13 06:41 PM, egg82 wrote: also, due to the nature of randomness, the first and last values are always going to have a lower chance of appearing than other values.

That's not true.

The random function() creates a uniformly distributed (pseudo) random number.
It's NOT a "less probable chance to get the first and last value" - distribution.

proof of concept:

private var _numbers:Vector.<uint> = new Vector.<uint>;

private function onEnterFrame(e:Event):void {
_numbers[Math.round(Math.random() * 9)]++;
}

output:

(0) : 276
(1) : 554
(2) : 535
(3) : 518
(4) : 548
(5) : 590
(6) : 544
(7) : 579
(8) : 557
(9) : 299

This proof is flawed by using round(), which either rounds up or down.
This basically shifts one half of the results by one slot, resulting in your posted output which does not represent the distribution.

Response to Random text in a movie clip. (AS3) 2013-02-13 09:51:20


At 2/13/13 09:48 AM, milchreis wrote: This proof is flawed by using round(), which either rounds up or down.

Yeah this is why you should cast to an int, instead of use the more expensive Math.round function.

Response to Random text in a movie clip. (AS3) 2013-02-13 11:08:31


At 2/13/13 09:48 AM, milchreis wrote: This proof is flawed by using round(), which either rounds up or down.
This basically shifts one half of the results by one slot, resulting in your posted output which does not represent the distribution.

I figured someone'd call me out on that - I worded it wrongly.
"Due to the nature of randomness" should be "Due to the nature of how we use randomness"

It's the same with two dice rolls. Separately, there's an equal chance of every number appearing. Together, however, the chance of 2 and 12 appearing is quite low compared to 7.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Random text in a movie clip. (AS3) 2013-02-13 13:21:14


At 2/13/13 11:08 AM, egg82 wrote: It's the same with two dice rolls. Separately, there's an equal chance of every number appearing. Together, however, the chance of 2 and 12 appearing is quite low compared to 7.

That's comparing apples and oranges.

Response to Random text in a movie clip. (AS3) 2013-02-13 13:28:18


At 2/13/13 01:21 PM, milchreis wrote: That's comparing apples and oranges.

I mean by comparing the use of random numbers. With randomness, each number is equally likely to present itself, however the way we generally use them makes this less likely so. Think of board games or games in general: stuff like using two or more dice or Math.round() decreases the chance of certain numbers appearing.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Random text in a movie clip. (AS3) 2013-02-13 13:38:22


At 2/13/13 01:28 PM, egg82 wrote: I mean by comparing the use of random numbers. With randomness, each number is equally likely to present itself, however the way we generally use them makes this less likely so.

Which is what I wrote in my first post in this thread.

Response to Random text in a movie clip. (AS3) 2013-02-13 13:41:05


At 2/13/13 01:38 PM, milchreis wrote: Which is what I wrote in my first post in this thread.

but i'm cooler, so therefore I win!

P

Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Random text in a movie clip. (AS3) 2013-02-13 13:42:11


At 2/13/13 01:41 PM, egg82 wrote:
P

right. Tongue face doesn't work in quotes.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature