Forum Topic: As: Using The Random Code!

(6,269 views • 38 replies)

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

<< < > >>
None

Reed

Reply To Post Reply & Quote

Posted at: 8/10/05 06:33 PM

Reed FAB LEVEL 18

Sign-Up: 03/25/04

Posts: 3,865

OMFG AS: MAINZ0R!!1
This is the ultimate how to make random things code. I see random movement was done, so I won't cover that. Lets start with random x and random y positions!

Random x and y positions
Start out by making a movieclip *draw something and hit F8* and give it the instance name of "Reed" without quotes. Now make a button *draw something, hit F8 and select button* and put this code in it *to open the AS window, hit F9*
CODE
on(release) {
_root.Reed._x = Math.random()*400;
_root.Reed._y = Math.random()*400;
}

Let me explain this, this is a simple cood meaning, when you release the button, the movie clip "Reed" moves to a random location between 1 and 400. It's a simple code that can be edited to make VERY cool things, like powerups. EXAMPLE

Random Numbers
This is a simple one too, in fact they're all pretty simple. Make a dynamic textbox *make a textbox, hit ctrl + F3 and switch to dynamic* and give it the instance and variable name "bb" without quotes. Now make a button under it and put this code in it:
CODE
on(release) {
_root.bb = (Math.round(Math.random()*10));
}

this means when you release the button, the textbox "bb" will generate a random number between 1 and 10. You can make it generate a random number with higher digits by raising the "*10" part of the code. Math.round makes it so it wont have a decimal.
NOTE: you can change "round" to "ceil" and "floor", round means round to the nearest, ceil means round up, and floor means round down.
EXAMPLE
GotoAndPlay RANDOM!
This will be the last part of the tutorial. What you need to do is make a movieclip, and put stop() in the first frame inside of it. After that, make10 frames, it doesn't matter what they are, I used differen't colors, and make a button (outside the mc) and put this code in it.
CODE
on(release) {
_root.gf.gotoAndStop(Math.round(Math.rando
m()*10));
}

This will make it go to a random frame between 1 and 10, again, you can change that by changing the number at the end, and you can change the way it rounds by switching round to ceil or floor. Thanks for reading my tutorial on random codes, I hope you learned something! Remember, you don't have to just stick with what I gave you, you can do all sorts of things, by editing the code! Good luck!
-Reedo11-


None

Reed

Reply To Post Reply & Quote

Posted at: 8/10/05 06:35 PM

Reed FAB LEVEL 18

Sign-Up: 03/25/04

Posts: 3,865

Oh and this is the example for the last one, sorry about that!


None

Rustygames

Reply To Post Reply & Quote

Posted at: 8/10/05 06:36 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

OMG I cant beleive you wated your time with this. A n00bs heaven for junk code

Hey how about

lineStyle (random (10),random (999999),random (100))
lineTo (random (550),random (400))

- Matt, Rustyarcade.com


None

Reed

Reply To Post Reply & Quote

Posted at: 8/10/05 06:39 PM

Reed FAB LEVEL 18

Sign-Up: 03/25/04

Posts: 3,865

At 8/10/05 06:36 PM, Ninja-Chicken wrote: OMG I cant beleive you wated your time with this. A n00bs heaven for junk code

That was the idea, you see, n00bs aren't always n00bs, if they try to learn! It's fun sometimes to help people, you should try it.


None

Rustygames

Reply To Post Reply & Quote

Posted at: 8/10/05 06:41 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

hmmm you mean with my AS: threads which utilise stuuf thats already been covered (like this one) however mine utilizes MANY AS topics and in a practical way. YOU utilize 1 crappy thing covered within another topic (AS:Math) and yet you say I dont help people?
Whatever be quiet and make an AS thread that actual convers something NEW

- Matt, Rustyarcade.com


None

Reed

Reply To Post Reply & Quote

Posted at: 8/10/05 07:03 PM

Reed FAB LEVEL 18

Sign-Up: 03/25/04

Posts: 3,865

At 8/10/05 06:41 PM, Ninja-Chicken wrote: hmmm you mean with my AS: threads which utilise stuuf thats already been covered (like this one) however mine utilizes MANY AS topics and in a practical way. YOU utilize 1 crappy thing covered within another topic (AS:Math) and yet you say I dont help people?
Whatever be quiet and make an AS thread that actual convers something NEW

They barely covered it in AS: Math, and I have seen your AS topics, on how to make a game, what a bad idea, I'm teaching, you're letting people copy and paste. You may be helping people too, but in my opinion, they'd skim your "AS: Game 1" copy and paste the code and viola! They now have a game, that they're going to submit. But I guess trying was a bad idea, so I'll shut up and not help out anymore, and leave this topic alone.


None

Denvish

Reply To Post Reply & Quote

Posted at: 8/10/05 07:17 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

At 8/10/05 06:41 PM, Ninja-Chicken wrote: Whatever be quiet and make an AS thread that actual convers something NEW

Quit with the attitude. Too much bitching going on in AS threads. People new to Flash may not be ready for AS: Movement - Random or AS: Maths yet. Topics that cover stuff like this are just as important as the more complex stuff.

At 8/10/05 06:33 PM, -Reedo11- wrote: This is the ultimate how to make random things code. I see random movement was done, so I won't cover that. Lets start with random x and random y positions!

Sorry I missed this on my AS: Main update, I'll put it in the Beginners section next time.

- - Flash - Music - Images - -

BBS Signature

None

Newbiesoup

Reply To Post Reply & Quote

Posted at: 10/6/05 09:05 AM

Newbiesoup LIGHT LEVEL 03

Sign-Up: 10/05/05

Posts: 1

Thanks -Reedo11- I found this really helpfull! Im still very new to flash and every little bit helps! Don't listen to people like Ninja-Chicken. I learnt something from it which is good enough for me :)


None

Go0gley

Reply To Post Reply & Quote

Posted at: 10/6/05 09:40 AM

Go0gley LIGHT LEVEL 19

Sign-Up: 07/08/04

Posts: 218

At 8/10/05 06:33 PM, -Reedo11- wrote: Let me explain this, this is a simple cood meaning, when you release the button, the movie clip "Reed" moves to a random location between 1 and 400.

It's 0 to 400.. unless you use ceil. Same for your 'random numbers' section. And actually... just random() is easier because it saves you from rounding.

on(release) {
_root.gf.gotoAndStop(Math.round(Math.rando

m()*10));

}

This will make it go to a random frame between 1 and 10, again, you can change that by changing the number at the end

Besides that, here's a script that adapts itself, and never returns 0.

on (release) {
_root.gf.gotoAndStop(random(_root.gf._tota
lframes)+1);
}

Oh and you forgot to explain what gf is... :P


None

Rantzien

Reply To Post Reply & Quote

Posted at: 10/6/05 09:46 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

At 10/6/05 09:40 AM, Go0gley wrote: It's 0 to 400..

Actually 0 <= n < 400

And actually... just random() is easier because it saves you from rounding.

But deprecated for some reason. The AS team recommends to use Math.random() instead, and I obey because I am their slave.

Besides that, here's a script that adapts itself, and never returns 0.

on (release) {
_root.gf.gotoAndStop(random(_root.gf._tota
lframes)+1);
}

or _root.gf.gotoAndStop(Math.ceil(Math.random
()*_root.gf._totalframes));

Oh and you forgot to explain what gf is... :P

Girlfriend? O_o
Just kidding

BBS Signature

None

Toast

Reply To Post Reply & Quote

Posted at: 10/6/05 11:24 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

At 8/10/05 07:17 PM, Denvish wrote: Sorry I missed this on my AS: Main update, I'll put it in the Beginners section next time.

Yeah, you didn't include AS: Combo neither...
I'll just wait and hope :)


None

liaaaam

Reply To Post Reply & Quote

Posted at: 10/6/05 11:25 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,530

At 10/6/05 09:46 AM, Rantzien wrote: But deprecated for some reason. The AS team recommends to use Math.random() instead, and I obey because I am their slave.

Theres really no point in using two functions rather than one, por ejemplo, whats easiest out of these two codes?

Math.round((Math.random()*100));

or

random(100);


None

Plebbi

Reply To Post Reply & Quote

Posted at: 4/16/06 03:45 PM

Plebbi LIGHT LEVEL 12

Sign-Up: 07/01/03

Posts: 197

I even learned from this :D.. im not much of a AS although i wrote the code for my own games :P. But learning about this AS Thread is something i really want to know :S.. i just stumbled on this :D its a good tutorial for beginners intermediate and experts and professionals :D


None

Blaze

Reply To Post Reply & Quote

Posted at: 4/16/06 03:59 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,989

At 8/10/05 06:33 PM, -Reedo11- wrote: OMFG AS: MAINZ0R!!1
This is the ultimate how to make random things code. I see random movement was done, so I won't cover that. Lets start with random x and random y positions!

What about random colors!

var colors:Array = new Array();
colors.push(0x000000);
colors.push(0xffffff);
colors.push(0xff0000);

onEnterFrame=function(){
moveTo(random(550),random(400));
lineStyle(2,random[colors.length],random(1
00));
lineTo(random(550),random(400));
}

I wonder if that would work, im not sure about the

lineStyle(2,random[colors.length],random(1
00));

Done it before, but i always forget.

Anyways, good tutorial Reed. :)


None

W35

Reply To Post Reply & Quote

Posted at: 5/28/06 03:04 PM

W35 EVIL LEVEL 37

Sign-Up: 10/14/03

Posts: 7,724

What if I wanted to get a random number between 5 and 10?

I'm so 1337, I'm 1338
My Site | My Digg | My Flickr | My Last.fm

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 5/28/06 03:17 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,370

Nope, capitals go only when you want to seperate words (hideBuiltInItems();) or for classes (BitmapData, ContextMenu).

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Toast

Reply To Post Reply & Quote

Posted at: 5/28/06 03:20 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

This tutorial simplified:

random(num) would return 0 to num-1.

edn :P


None

W35

Reply To Post Reply & Quote

Posted at: 5/28/06 03:33 PM

W35 EVIL LEVEL 37

Sign-Up: 10/14/03

Posts: 7,724

At 5/28/06 03:12 PM, C10H14N2 wrote: var BetweenVifeAndTen:Number = Math.round(Math.random()*5+1)+5;

Thanks hombre.

I'm so 1337, I'm 1338
My Site | My Digg | My Flickr | My Last.fm

BBS Signature

None

Paranoia

Reply To Post Reply & Quote

Posted at: 5/28/06 03:37 PM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,697

At 5/28/06 03:20 PM, -Toast- wrote: This tutorial simplified:

random(num) would return 0 to num-1.

Hasn't random() been deprecated though :P? If you still wanted to use it you could just make a function:

function rndm (inPt:Number) : Number{
return Math.round(Math.random() * inPt);
}
trace(rndm(5));

BBS Signature

None

Toast

Reply To Post Reply & Quote

Posted at: 5/28/06 05:22 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

Well Flash 8 supports random(), I don't see why we shouldn't use it. Flash 9 possibly won't though.


None

Chaz

Reply To Post Reply & Quote

Posted at: 5/28/06 06:07 PM

Chaz NEUTRAL LEVEL 22

Sign-Up: 09/27/05

Posts: 4,323

I liked this tutorial, useful for getting into this kinda basic method thing.


None

Dark-Scythes

Reply To Post Reply & Quote

Posted at: 6/15/06 11:41 PM

Dark-Scythes NEUTRAL LEVEL 03

Sign-Up: 06/15/06

Posts: 225

Thanks for making this tutorial, It REALY helped me. Great job on it.


None

Rammstein911

Reply To Post Reply & Quote

Posted at: 8/22/06 03:08 PM

Rammstein911 NEUTRAL LEVEL 15

Sign-Up: 01/07/05

Posts: 42

Hm.. what about randomly playing a movie clip? Or random creation of a movie clip that appears in a random position?

Don't mean to be tedious or anything..

-R911


None

DarkMana

Reply To Post Reply & Quote

Posted at: 8/22/06 03:52 PM

DarkMana DARK LEVEL 23

Sign-Up: 04/04/05

Posts: 2,727

You could make a more complicated funtion that generates a random number between two:

function rndm(a:Number, b:Number) {
if (a > b) {
trace("a must be smaller than or equal to b");
} else {
c = b-a;
d = Math.round(Math.random()*c+a);
return d;
}
}

Where 'a' and 'b' are the number between which a random number (d) will be generated.

Asus P5Q PRO, Intel E8400 @ 3.60 GHz, 4GB DDR2-1000, ATI HD4850

BBS Signature

None

DarkMana

Reply To Post Reply & Quote

Posted at: 8/22/06 03:57 PM

DarkMana DARK LEVEL 23

Sign-Up: 04/04/05

Posts: 2,727

At 8/22/06 03:52 PM, Dark-Mana wrote: funtion

zomg typo! :O

Asus P5Q PRO, Intel E8400 @ 3.60 GHz, 4GB DDR2-1000, ATI HD4850

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 8/22/06 03:59 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

At 8/22/06 03:52 PM, Dark-Mana wrote: trace("a must be smaller than or equal to b");

Mwaaha, you could ofcourse just do

function rand(a:Number,b:Number):Number
{
if(a<b) return Math.round(Math.random()*(b-a))+a;
else return Math.round(Math.random()*(a-b))+b;
}

or if you want to use random(), which i would recomend pre AS3 since its been removed then

function rand(a:Number, b:Number):Number
{
if(a<b) return random(b-a)+a;
else return random(a-b)+b;
}

My social worker says im special!

BBS Signature

None

DarkMana

Reply To Post Reply & Quote

Posted at: 8/22/06 04:05 PM

DarkMana DARK LEVEL 23

Sign-Up: 04/04/05

Posts: 2,727

At 8/22/06 03:59 PM, -dELta- wrote:
At 8/22/06 03:52 PM, Dark-Mana wrote: trace("a must be smaller than or equal to b");
Mwaaha, you could ofcourse just do

function rand(a:Number,b:Number):Number
{
if(a<b) return Math.round(Math.random()*(b-a))+a;
else return Math.round(Math.random()*(a-b))+b;
}

But that makes it idiot-proof. Do we really want every idiot playing with random number functions?

lol

Asus P5Q PRO, Intel E8400 @ 3.60 GHz, 4GB DDR2-1000, ATI HD4850

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 8/22/06 04:09 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

wait, you dont even need to do that when using Math.random()

when using Math.random() you could just use

function rand(a:Number, b:Number):Number
{
return Math.round(Math.random()*(b-a)) + a;
}

in the case that a>b, it simply means that the random part, will be negative, and cause it to be between a and b anyways

My social worker says im special!

BBS Signature

Happy

Andy-Smithy

Reply To Post Reply & Quote

Posted at: 8/22/06 04:18 PM

Andy-Smithy LIGHT LEVEL 09

Sign-Up: 07/27/06

Posts: 659

At 8/10/05 06:36 PM, Ninja-chicken wrote: OMG I cant beleive you wated your time with this. A n00bs heaven for junk code

No matter what Ninja_Chícken says, i foud it helpful! :D
Oh, and Ninja_chicken, how else are people like me meant to learn if there is no example...


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 8/22/06 04:20 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

At 8/22/06 04:18 PM, Andy_Smithy wrote: how else are people like me meant to learn if there is no example...

Uuuuuhh.... use flashes documentation? you know, it does have an example of almost every single thing in the language?

My social worker says im special!

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 01:43 AM

<< 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!