Forum Topic: As: More Random Codes

(3,920 views • 32 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
Happy

SpamBurger

Reply To Post Reply & Quote

Posted at: 10/29/05 05:13 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

AS: Main
---------------
First off, you will need to learn about arrays and the random code.
---------------
What you will learn
In this tutorial, you will learn 2 things to do with the random code. The first thing you will learn is how to make a button play frames at random. IE- A button will randomly choose between frame numbers 2, 5, and 6. It will then play that frame. The second thing you will learn is how to make random words appear in text boxes.
---------------
Random Frame Thing
First off, make a button. You do this by drawing it, right clicking, selecting "Convert To Symbol" and then selecting button. After, that, give your button this code:

on(press){
framenum=new Array(frame1,frame2,frame3);
gotoAndPlay(framenum[random(3)]);
}

Important!!
Make sure you replace frame1, frame2, and frame 3 with 3 different frames of your choice. Also, make sure you have that many frames on your timeline. :-P
---------------
What It Does
First, when you press your button, it will create a new array named framenum. You can change framenum to whatever you want but make sure you change all traces of it in the code to the new value. Second, the button will play the specified numbers within the array at random. If you wanna add more values, just add some more numbers into the array and than change random(3) to the amount of values in the array.
---------------
Making Random Words Appear
I assume you know how to give textboxes random number values, but now you will learn how to give them random word values. First, make a dynamic text box with the var name of "wordz" without the quotes. Then, give the frame this code:

words=new Array("Bob","Joe","Daniel","SpamBurger");
_root.wordz=words[random(4)];
---------------
What It Does
First, flash will create a new array called words. Then, flash will give the dynamic text box, wordz, the value of words. It will choose randomly between one of the values within the array. As you can see, instead of giving the array number values, we give it word values, or as we flash junkies would call it, strings. Whenever using a string, you must give it quotes.
---------------
Button that changes it's value
Now, I will teach you how to make a button that will change its value. Like the first button code, it looks like this:

on(press){
_root.wordz=_root.words[random(4)];
}

Im assuming that would work but I created it right here on the BBS so I dont know.
---------------
How It Works
First, flash will perform the action when you press the button. Then, it will give the dynamic text box "wordz" a new value from the array of "words".
---------------
~Please post here any comments or suggestions.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Toast

Reply To Post Reply & Quote

Posted at: 10/29/05 05:15 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,917

Not bad, but completley useless.

on(press){
framenum=new Array(frame1,frame2,frame3);
gotoAndPlay(framenum[random(3)]);
}

Can be done in this way:

on(press){
gotoAndPlay(random(2) + 2;
}


None

sosjulian

Reply To Post Reply & Quote

Posted at: 10/29/05 05:15 PM

sosjulian NEUTRAL LEVEL 11

Sign-Up: 06/14/05

Posts: 676

knock knock

who's there?
postt

post who?

post this somewhere else!


None

Toast

Reply To Post Reply & Quote

Posted at: 10/29/05 05:16 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,917

Whoops.


on(press){
gotoAndPlay(random(3)+ 1);
}

This works.


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 10/29/05 05:18 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 10/29/05 05:15 PM, sosjulian wrote: knock knock

knock knock


who's there?
postt

No, SpamBurger.


post who?

This Post! (Im so smart!)


post this somewhere else!

Post a dick up your ass why dont ya!

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Kipling

Reply To Post Reply & Quote

Posted at: 10/29/05 05:20 PM

Kipling NEUTRAL LEVEL 13

Sign-Up: 05/04/04

Posts: 2,641

Hello. What am I doing here?


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 10/29/05 05:21 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 10/29/05 05:15 PM, -Toast- wrote: blah

Why do I always seem to do everything the hard way!?

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Toast

Reply To Post Reply & Quote

Posted at: 10/29/05 05:34 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,917

What the heck? I think making arrays is NOT easier than using a very simple random script...


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 10/29/05 05:36 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 10/29/05 05:34 PM, -Toast- wrote: What the heck? I think making arrays is NOT easier than using a very simple random script...

Exactly, but I did use arrays so I said "Why do I always do it the hard way!?"

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


Happy

JPI

Reply To Post Reply & Quote

Posted at: 11/28/05 07:21 PM

JPI DARK LEVEL 21

Sign-Up: 05/09/05

Posts: 3,132

At 10/29/05 05:21 PM, SpamBurger wrote:
At 10/29/05 05:15 PM, -Toast- wrote: blah
Why do I always seem to do everything the hard way!?

Fuck that, this is great.
It solves my problem,
It enables me to make a list of frames, radomise it, then delete one, then radomise it again, then delete one. So that you only have the frame displayed once!

THANK YOU SPAM BURGER!

SIG YOINK!

BBS Signature

None

MetalMiX9994

Reply To Post Reply & Quote

Posted at: 12/2/05 06:03 PM

MetalMiX9994 EVIL LEVEL 03

Sign-Up: 05/18/05

Posts: 12

wowowowow this topic is spamed up =)


None

Creepy

Reply To Post Reply & Quote

Posted at: 12/2/05 06:12 PM

Creepy EVIL LEVEL 07

Sign-Up: 11/28/04

Posts: 1,241

At 10/29/05 05:21 PM, SpamBurger wrote:
At 10/29/05 05:15 PM, -Toast- wrote: blah
Why do I always seem to do everything the hard way!?

you dont, cuz if someone wanna goto one of the random frames 4, 58 and 134 you cant say gotoAndPlayrandom (4 or 58 or 134)
then you make an array

var array = new Array(4, 58, 134);
gotoAndPlay(array[random(array.length)]);


None

Claxor

Reply To Post Reply & Quote

Posted at: 12/2/05 06:14 PM

Claxor DARK LEVEL 10

Sign-Up: 10/21/05

Posts: 2,465

At 12/2/05 06:12 PM, Creeepy wrote: you dont

Or if you use labels

BBS Signature

None

Blaze

Reply To Post Reply & Quote

Posted at: 12/2/05 06:15 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,989

Well, good tut Spam. Can be useful at times, i guess.

Im not much familiar with Arrays, so this kinda helps. *bookmarks*


None

authorblues

Reply To Post Reply & Quote

Posted at: 12/2/05 06:15 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

At 12/2/05 06:12 PM, Creeepy wrote: var array = new Array(4, 58, 134);
gotoAndPlay(array[random(array.length)]);

yeah, well i contend that your methods are... the pits:
var framesArray:Array = new Array(4, 58, 134);
gotoAndPlay(framesArray[Math.ceil(Math.ran
dom()*framesArray.length)]);

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Creepy

Reply To Post Reply & Quote

Posted at: 12/2/05 06:24 PM

Creepy EVIL LEVEL 07

Sign-Up: 11/28/04

Posts: 1,241

At 12/2/05 06:15 PM, authorblues wrote:
At 12/2/05 06:12 PM, Creeepy wrote: var array = new Array(4, 58, 134);
gotoAndPlay(array[random(array.length)]);
yeah, well i contend that your methods are... the pits:
var framesArray:Array = new Array(4, 58, 134);
gotoAndPlay(framesArray[Math.ceil(Math.ran
dom()*framesArray.length)]);

tell me, whats the different =P
I know i should have used strict dat typing to the array (cuz then its comfortable with oop)
but the random scripts does the same, but mine is smaller.


None

Claxor

Reply To Post Reply & Quote

Posted at: 12/2/05 06:26 PM

Claxor DARK LEVEL 10

Sign-Up: 10/21/05

Posts: 2,465

At 12/2/05 06:15 PM, authorblues wrote: var framesArray:Array = new Array(4, 58, 134);
gotoAndPlay(framesArray[Math.ceil(Math.ran
dom()*framesArray.length)]);

Shouldn't it be Math.floor, as arrays are zero-based?

BBS Signature

None

Lord-Sonx

Reply To Post Reply & Quote

Posted at: 12/2/05 06:28 PM

Lord-Sonx EVIL LEVEL 32

Sign-Up: 01/17/04

Posts: 8,871

it'd be nice to know if these AS codes work in flash 6,7,8 or other...

dont' think i've seen any sort of flash version indication in most htese AS thingys..


None

Creepy

Reply To Post Reply & Quote

Posted at: 12/2/05 06:31 PM

Creepy EVIL LEVEL 07

Sign-Up: 11/28/04

Posts: 1,241

At 12/2/05 06:28 PM, Lord_Sonx wrote: it'd be nice to know if these AS codes work in flash 6,7,8 or other...

yes all this codes work since mx 2004, i dont think u can use strict data typing in mc though

dont' think i've seen any sort of flash version indication in most htese AS thingys..

huh?


None

Creepy

Reply To Post Reply & Quote

Posted at: 12/2/05 06:34 PM

Creepy EVIL LEVEL 07

Sign-Up: 11/28/04

Posts: 1,241

At 12/2/05 06:26 PM, Claxor wrote: Shouldn't it be Math.floor, as arrays are zero-based?

why do u acctually need to round that number up at all?
is there anything im missing here?


None

authorblues

Reply To Post Reply & Quote

Posted at: 12/2/05 06:41 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

At 11/28/05 07:21 PM, James_Prankard_Inc wrote: It enables me to make a list of frames, radomise it, then delete one, then radomise it again, then delete one. So that you only have the frame displayed once!

i felt like writing this code, as a resource (and FOSS) for those who need it
(feel free to adjust where necessary):

var frameArray:Array = new Array(23, 1, 34, 18, 27, 31, 42);
function randFrame():Void{
var whereFrame:Number = Math.floor(Math.random()*frameArray.length
)
_root.gotoAndStop(frameArray[whereFrame]);
frameArray.splice(whereFrame, 1);
}
randTimer = setInterval(randFrame, 2000);

this command will goto a random frame out of the array every 2 seconds, and it selects the frame from the array (with removal)...

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

authorblues

Reply To Post Reply & Quote

Posted at: 12/2/05 06:43 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

At 12/2/05 06:26 PM, Claxor wrote: Shouldn't it be Math.floor, as arrays are zero-based?

yes, i always do that. i was really trying to do two thing only:

1. point out that you forgot to specify your datatype for your variable
2. start a discussion on the difference between:

random(num) AND Math.floor(Math.random()*num)

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

IWantSomeCookies

Reply To Post Reply & Quote

Posted at: 12/2/05 06:50 PM

IWantSomeCookies LIGHT LEVEL 13

Sign-Up: 08/20/04

Posts: 3,295

I guess this is a neat code! Nice tute Spam, but I guess your name is leading people to post spam in here >:(

"Actually, the server timed out trying to remove all your posts..."
-TomFulp


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 12/2/05 07:05 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 12/2/05 06:50 PM, IWantSomeCookies wrote: I guess this is a neat code! Nice tute Spam, but I guess your name is leading people to post spam in here >:(

Yea, but I want people to realize that my name means the food spam, not the other type of spam.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Fire

Reply To Post Reply & Quote

Posted at: 12/10/05 12:32 PM

Fire DARK LEVEL 15

Sign-Up: 10/29/03

Posts: 2,935

At 10/29/05 05:15 PM, -Toast- wrote: Can be done in this way:

on(press){
gotoAndPlay(random(2) + 2;
}

See AS:Bad Ideas for why I am changing this script

on(press){
gotoAndPlay(Math.random(2)+2);
}


None

liaaaam

Reply To Post Reply & Quote

Posted at: 12/10/05 12:35 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,534

At 12/10/05 12:32 PM, GameCubeFreak2004 wrote: on(press){
gotoAndPlay(Math.random(2)+2);
}

That won't work, you want

on(press){
gotoAndPlay(Math.floor(Math.random()*2)+2)
;
}


Happy

HPen

Reply To Post Reply & Quote

Posted at: 5/28/07 09:54 AM

HPen LIGHT LEVEL 02

Sign-Up: 11/15/05

Posts: 79

Can someone please tell me the script where flash randomly chooses between 3 frames like frame 24 frame 35 and frame 99?

I only ask because I have Flash MX and you code for the random selection between the 3 frames only works for Flash MX 2004 and so on...


None

HPen

Reply To Post Reply & Quote

Posted at: 5/28/07 11:00 AM

HPen LIGHT LEVEL 02

Sign-Up: 11/15/05

Posts: 79

Come on party people :D


None

UnknownFear

Reply To Post Reply & Quote

Posted at: 6/10/07 08:34 AM

UnknownFear FAB LEVEL 34

Sign-Up: 02/27/04

Posts: 6,912

Can this code also work for making random enemys appear? Sorry for being noobish.

Blog
[ T | F | G/MSN ]
--Ubuntu 9.10 "Karmic Koala" User--


None

El-Presidente

Reply To Post Reply & Quote

Posted at: 6/10/07 08:37 AM

El-Presidente LIGHT LEVEL 27

Sign-Up: 06/02/05

Posts: 5,186

You could have explained it more in depth, but good job.

MY E-PENIS IS BIGGER THAN YOURS
8=================================>
...and this is my fag...

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 03:18 PM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!