Be a Supporter!

Random text in a movie clip. (AS3)

  • 448 Views
  • 20 Replies
New Topic Respond to this Topic
edhe
edhe
  • Member since: Jun. 6, 2003
  • Offline.
Forum Stats
Member
Level 23
Animator
Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 05:58 PM Reply

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

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 06:10 PM Reply

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
edhe
edhe
  • Member since: Jun. 6, 2003
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 06:11 PM Reply

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.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 06:21 PM Reply

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
edhe
edhe
  • Member since: Jun. 6, 2003
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 06:22 PM Reply

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.

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 06:26 PM Reply

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.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 06:27 PM Reply

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
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 06:41 PM Reply

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
edhe
edhe
  • Member since: Jun. 6, 2003
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 06:43 PM Reply

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.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 06:53 PM Reply

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
edhe
edhe
  • Member since: Jun. 6, 2003
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 07:05 PM Reply

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.

edhe
edhe
  • Member since: Jun. 6, 2003
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to Random text in a movie clip. (AS3) Feb. 12th, 2013 @ 07:08 PM Reply

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.

w7y7a7t7t
w7y7a7t7t
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 02
Programmer
Response to Random text in a movie clip. (AS3) Feb. 13th, 2013 @ 12:58 AM Reply

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.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Random text in a movie clip. (AS3) Feb. 13th, 2013 @ 09:48 AM Reply

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.

Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to Random text in a movie clip. (AS3) Feb. 13th, 2013 @ 09:51 AM Reply

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.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Random text in a movie clip. (AS3) Feb. 13th, 2013 @ 11:08 AM Reply

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
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Random text in a movie clip. (AS3) Feb. 13th, 2013 @ 01:21 PM Reply

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.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Random text in a movie clip. (AS3) Feb. 13th, 2013 @ 01:28 PM Reply

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
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Random text in a movie clip. (AS3) Feb. 13th, 2013 @ 01:38 PM Reply

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.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Random text in a movie clip. (AS3) Feb. 13th, 2013 @ 01:41 PM Reply

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
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Random text in a movie clip. (AS3) Feb. 13th, 2013 @ 01:42 PM Reply

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