Forum Topic: AS: Random

(4,835 views • 33 replies)

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

<< < > >>
None

Begoner

Reply To Post Reply & Quote

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

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

Like AS: Basic Movement, only with codes that involve something random. A code for a scrolling starfield:

_root.createEmptyMovieClip("screen_mc", 1); with (screen_mc) { moveTo(0, 0); lineStyle(0, 000000, 100); beginFill(000000); lineTo(0, 400); lineTo(550, 400); lineTo(550, 0); lineTo(0, 0); endFill(); } _root.createEmptyMovieClip("star_mc", 2); with (star_mc) { lineStyle(0, 0xFFFFFF, 100); beginFill(0xFFFFFF); moveTo(0, 0); lineTo(1, 0); lineTo(1, 1); lineTo(0, 1); lineTo(0, 0); endFill(); } inc = 1; for (i=1; i<250; i++) { duplicateMovieClip(star_mc, "star_mc"+i, 2+i); _root["star_mc"+i]._x = random(550); _root["star_mc"+i]._y = random(400); _root["star_mc"+i]._xscale = _root["star_mc"+i]._yscale=random(200); _root["star_mc"+i]._alpha = random(50)+50; } createEmptyMovieClip("text_mc", 300); with (text_mc) { moveTo(0, 0); lineStyle(0, 000000, 100); beginFill(0x27D341); lineTo(25, 0); lineTo(25, 20); lineTo(0, 20); lineTo(0, 0); endFill(); _root["text_mc"].onPress = function() { for (i=1; i<250; i++) { _root["star_mc"+i]._x = random(550); _root["star_mc"+i]._y = random(400); } }; } _root.onEnterFrame = function() { if (Key.isDown(Key.LEFT)) { if (inc>0) { inc--; } } else if (Key.isDown(Key.RIGHT)) { inc++; } for (i=1; i<250; i++) { _root["star_mc"+i]._y += inc; if (_root["star_mc"+i]._y>=400) { _root["star_mc"+i]._y = 0; } } if (Key.isDown(Key.ENTER)) { for (i=1; i<250; i++) { _root["star_mc"+i]._x = random(550); _root["star_mc"+i]._y = random(400); } } };

Paste on first frame of movie. Use left/right to make the starfield scroll slower/faster. Use the green button or enter to get a new starfield.


None

Denvish

Reply To Post Reply & Quote

Posted at: 2/11/05 06:18 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

At 2/11/05 06:12 PM, Begoner wrote:

Cool, but could do with a little formatting and some kind of explanation as to how/why it works. Nevertheless, I'll add it to the AS: Main list when I next update it.

- - Flash - Music - Images - -

BBS Signature

None

Begoner

Reply To Post Reply & Quote

Posted at: 2/11/05 07:00 PM

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

Alright.

_root.createEmptyMovieClip("screen_mc", 1);
with (screen_mc) {
moveTo(0, 0);
lineStyle(0, 000000, 100);
beginFill(000000);
lineTo(0, 400);
lineTo(550, 400);
lineTo(550, 0);
lineTo(0, 0);
endFill();
}

This makes the black background.

_root.createEmptyMovieClip("star_mc", 2);
with (star_mc) {
lineStyle(0, 0xFFFFFF, 100);
beginFill(0xFFFFFF);
moveTo(0, 0);
lineTo(1, 0);
lineTo(1, 1);
lineTo(0, 1);
lineTo(0, 0);
endFill();
}

This makes one star.

inc = 1;

Sets the speed of movement.

for (i=1; i<250; i++) {
duplicateMovieClip(star_mc, "star_mc"+i, 2+i);
_root["star_mc"+i]._x = random(550);
_root["star_mc"+i]._y = random(400);
_root["star_mc"+i]._xscale = _root["star_mc"+i]._yscale=random(200);
_root["star_mc"+i]._alpha = random(50)+50;
}

Duplicates the movie clip 250 times, and gives each star a random x and y position, a random size, and a random brightness.

createEmptyMovieClip("text_mc", 300);
with (text_mc) {
moveTo(0, 0);
lineStyle(0, 000000, 100);
beginFill(0x27D341);
lineTo(25, 0);
lineTo(25, 20);
lineTo(0, 20);
lineTo(0, 0);
endFill();

Makes the reset button.

_root["text_mc"].onPress = function() {
for (i=1; i<250; i++) {
_root["star_mc"+i]._x = random(550);
_root["star_mc"+i]._y = random(400);
}
};

Makes it so that each star has a random position when the button is pressed.

}
_root.onEnterFrame = function() {
if (Key.isDown(Key.LEFT)) {
if (inc>0) {
inc--;
}

Lowers the speed.

} else if (Key.isDown(Key.RIGHT)) {
inc++;
}

Raises the speed.

for (i=1; i<250; i++) {
_root["star_mc"+i]._y += inc;
if (_root["star_mc"+i]._y>=400) {
_root["star_mc"+i]._y = 0;
}
}

If a star gets to the bottom of the stage, it goes back to the top.

if (Key.isDown(Key.ENTER)) {
for (i=1; i<250; i++) {
_root["star_mc"+i]._x = random(550);
_root["star_mc"+i]._y = random(400);
}
}

Resets when enter is pressed, like reset button.

};


None

Denvish

Reply To Post Reply & Quote

Posted at: 2/11/05 08:06 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

At 2/11/05 07:00 PM, Begoner wrote: Alright.

Cheers man, it's a lot clearer now.

- - Flash - Music - Images - -

BBS Signature

None

Taylor

Reply To Post Reply & Quote

Posted at: 2/11/05 08:11 PM

Taylor LIGHT LEVEL 09

Sign-Up: 08/19/03

Posts: 8,508

At 2/11/05 08:06 PM, Denvish wrote:
At 2/11/05 07:00 PM, Begoner wrote: Alright.
Cheers man, it's a lot clearer now.

Thats a suprisingly thorough explaniation...
+10 cool points for Begoner


None

StarCleaver

Reply To Post Reply & Quote

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

StarCleaver LIGHT LEVEL 29

Sign-Up: 01/03/03

Posts: 10,102

If you speed the starfield up a lot you just get lines of stars, not random stars.

I could surely die
If I only had some pie
Club-a-Club Club, son

BBS Signature

None

StarCleaver

Reply To Post Reply & Quote

Posted at: 2/11/05 11:23 PM

StarCleaver LIGHT LEVEL 29

Sign-Up: 01/03/03

Posts: 10,102

At 2/11/05 07:00 PM, Begoner wrote: for (i=1; i<250; i++) {
_root["star_mc"+i]._y += inc;
if (_root["star_mc"+i]._y>=400) {
_root["star_mc"+i]._y = 0;
}
}

Change this to:

for (i=1; i<250; i++) {
_root["star_mc"+i]._y += inc;
if (_root["star_mc"+i]._y>=400) {
_root["star_mc"+i]._y = random(400)*-1;
}
}

That way, the stars appear at a random position above the screen. So when they come down into view, they are random.

I could surely die
If I only had some pie
Club-a-Club Club, son

BBS Signature

None

Begoner

Reply To Post Reply & Quote

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

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

No, if you speed it up, the stars are not random. Their x position is the same, but the y position is set to 0. They come up as lines because as you speed it up, it takes less of a distance away from the bottom of the screen to go to the top.

For example, if the increment was 1, in 1 second, stars from 370-400 would be set to 0.

If it was 2, stars from 340-400 would be set to 0.

As the numbers get bigger and bigger, you would get stars from 200-400 being set to 0. Since the stars from 200-400 are condensed to a single y position, it forms a single line. I could change that, but that would make a random starfield, not the same one repeated over and over.


None

liaaaam

Reply To Post Reply & Quote

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

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,534

Erm, great code.. definatly. Didn't like the green box or the enter button function, so I got rid of them!

_root.createEmptyMovieClip("screen_mc", 1);
with (screen_mc) {
moveTo(0, 0);
lineStyle(0, 000000, 100);
beginFill(000000);
lineTo(0, 400);
lineTo(550, 400);
lineTo(550, 0);
lineTo(0, 0);
endFill();
}
_root.createEmptyMovieClip("star_mc", 2);
with (star_mc) {
lineStyle(0, 0xFFFFFF, 100);
beginFill(0xFFFFFF);
moveTo(0, 0);
lineTo(1, 0);
lineTo(1, 1);
lineTo(0, 1);
lineTo(0, 0);
endFill();
}
inc = 1;
for (i=1; i<250; i++) {
duplicateMovieClip(star_mc, "star_mc"+i, 2+i);
_root["star_mc"+i]._x = random(550);
_root["star_mc"+i]._y = random(400);
_root["star_mc"+i]._xscale = _root["star_mc"+i]._yscale=random(200);
_root["star_mc"+i]._alpha = random(50)+50;
}
_root.onEnterFrame = function() {
if (Key.isDown(Key.LEFT)) {
if (inc>0) {
inc--;
}
} else if (Key.isDown(Key.RIGHT)) {
inc++;
}
for (i=1; i<250; i++) {
_root["star_mc"+i]._y += inc;
if (_root["star_mc"+i]._y>=400) {
_root["star_mc"+i]._y = 0;
}
}
};

da da!


None

Begoner

Reply To Post Reply & Quote

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

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

Noooooo! Not the green box! My presssssssious!


None

liaaaam

Reply To Post Reply & Quote

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

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,534

At 2/12/05 05:24 PM, Begoner wrote: Noooooo! Not the green box! My presssssssious!

LMAO, the green box sucked. What was the point of it? Lol, anyway I thought the code was great and I could see what everything did as soon as I looked at it, well when it was formatted lol. A beginner or someone who doesn't know AS will find it strange though lol.


None

Begoner

Reply To Post Reply & Quote

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

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

Lol, thanks. I was going to make it possible to enter a speed in the green box, but then I decided to use the left/right arrows. Not wanting to throw away 5 mins. of my life, though, I decided to make it a reset button. :D


None

liaaaam

Reply To Post Reply & Quote

Posted at: 2/13/05 08:20 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,534

Yeah.. im bumping this code because its good. Ya.


None

Begoner

Reply To Post Reply & Quote

Posted at: 2/13/05 04:56 PM

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

More random things. This is a simple code for creating random terrain. Paste in first frame of movie.

peak1X = Math.random()*50+50;
peak1Y = Math.random()*325+50;
peak2X = Math.random()*50+150;
peak2Y = peak1Y=Math.random()*325+50;
peak3X = Math.random()*50+250;
peak3Y = peak1Y=Math.random()*325+50;
peak4X = Math.random()*50+350;
peak4Y = peak1Y=Math.random()*325+50;
peak5X = Math.random()*50+450;
peak5Y = peak1Y=Math.random()*325+50;
createEmptyMovieClip("screen", 1);
with (screen) {
moveTo(550, 250);
lineStyle(5, 0x000000);
beginFill(0x996633);
lineTo(550, 400);
lineTo(0, 400);
lineTo(0, 250);
lineTo(peak1X, peak1Y);
lineTo(peak2X, peak2Y);
lineTo(peak3X, peak3Y);
lineTo(peak4X, peak4Y);
lineTo(peak5X, peak5Y);
lineTo(550, 250);
endFill();
}


None

Begoner

Reply To Post Reply & Quote

Posted at: 2/13/05 05:30 PM

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

Here's a web version:

http://www.freewebs.com/begoner/terrainGen.swf

Press enter to reset terrain.


Thinking

RuneAura

Reply To Post Reply & Quote

Posted at: 2/13/05 07:02 PM

RuneAura EVIL LEVEL 08

Sign-Up: 08/23/04

Posts: 213

You have considered making a tutorial right?

One of the things I tried to teach myself (and after much time did) was how to draw in actionscript. You could easily make one of these and I think everyone whould be interested on how to do it.

Well anyways that's just one guy's opinion. If you do newgroundites will love you. Of course why not let 'em suffer like me while i tried to figure out how to do it?

And as he lay dying he whispered,
"I wonder if they'll write a song about me."


None

Begoner

Reply To Post Reply & Quote

Posted at: 2/13/05 07:41 PM

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

I would make a tutorial on drawing in AS, but I don't know how to that well. The first time I saw an AS drawing was a Paint program in Flash a week ago. And so there are lots of things that I'm bound to omit/misstate. If I learn AS drawing better, though, I will make a tutorial on it.


None

Begoner

Reply To Post Reply & Quote

Posted at: 2/14/05 05:49 PM

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

Alright, well, I made a tutorial to the best of my knowledge here:

http://www.freewebs.com/begoner/triangleTutorialTxT.htm

Please tell me if I made any errors so that I can change it.


None

Begoner

Reply To Post Reply & Quote

Posted at: 3/12/05 06:19 PM

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

Random scribbling, with random color, size, and alpha. Copy and paste into first frame of movie:

stop();
currX = Stage.width/2;
currY = Stage.height/2;
moveTo(currX, currY);
_root.onEnterFrame = function() {
lineStyle(Math.random()*10, Math.round(Math.random()*0xFFFFFF), Math.random()*100);
nextY = currY+Math.random()*40-20;
nextX = currX+Math.random()*40-20;
while (nextY<0) {
nextY += Math.random()*20;
}
while (nextY>Stage.height) {
nextY -= Math.random()*20;
}
while (nextX<0) {
nextX += Math.random()*20;
}
while (nextX>Stage.width) {
nextY -= Math.random()*20;
}
lineTo(nextX, nextY);
currX = nextX;
currY = nextY;
};


None

Remiraz

Reply To Post Reply & Quote

Posted at: 6/28/05 09:13 AM

Remiraz EVIL LEVEL 07

Sign-Up: 06/14/03

Posts: 248

whats the performance on this one? any better than using a standard jpeg background scrolling?


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 7/27/05 04:29 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,546

At 2/13/05 04:56 PM, Begoner wrote: More random things. This is a simple code for creating random terrain. Paste in first frame of movie.

same code but edited
<====== (i havnt got flash with me atm so i havnt tested this to see what it looks like)

var peaks:Number = 10;
//
var xinc:Number = 550/peaks;
var x2:Number = xinc/2;
function ranx(Void):Number { return Math.random()*xinc - x2 };
function rany(Void):Number { return Math.random()*325 + 50 };
//
var ter:MovieClip = createEmptyMovieClip("ground", 1);
ter.lineStyle(5, 0x000000, 100);
ter.beginFill(0x996633, 100);
ter.moveTo(0, 400);
//
var i:Number;
for(i=0;i<peaks;i++)
{
ter.lineTo( ranx()+xinc*i, rany() );
}
ter.lineTo(550, 400);
ter.lineTo(0, 400);
ter.endFill():

My social worker says im special!

BBS Signature

None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 7/27/05 05:01 AM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

about your first code theres a few optimization problems. like you have what 4/5 for loops? it got it down to 2. for loops, espescially over 100 instances, cause massive lag. also i shortened the code bit. i used binary values for some stuff, and lineStyle(0,"0xFFFFFF",100) is the default line style, takeing it out wouldn't change anything. i guess im just obsessed with shortening codes. whatever heres the new code.
_root.createEmptyMovieClip("screen_mc", 1);
with (screen_mc) {
moveTo(0, 0);
beginFill(000000);
lineTo(0, 400);
lineTo(550, 400);
lineTo(550, 0);
lineTo(0, 0);
endFill();
}
_root.createEmptyMovieClip("star_mc", 2);
with (star_mc) {
beginFill(0xFFFFFF);
moveTo(0, 0);
lineTo(1, 0);
lineTo(1, 1);
lineTo(0, 1);
lineTo(0, 0);
endFill();
}
inc = 1;
createEmptyMovieClip("text_mc", 300);
with (text_mc) {
moveTo(0, 0);
beginFill("0x27D341");
lineTo(25, 0);
lineTo(25, 20);
lineTo(0, 20);
lineTo(0, 0);
endFill();
}
for (i=1; i<250; i++) {
duplicateMovieClip(star_mc, "star_mc"+i, 2+i);
_root["star_mc"+i]._x = random(550);
_root["star_mc"+i]._y = random(400);
_root["star_mc"+i]._xscale = _root["star_mc"+i]._yscale=random(200);
_root["star_mc"+i]._alpha = random(50)+50;
_root["text_mc"].onPress = function() {
_root["star_mc"+i]._x = random(550);
_root["star_mc"+i]._y = random(400);
};
}
_root.onEnterFrame = function() {
inc += (Key.isDown(Key.RIGHT)-(Key.isDown(Key.LEF
T) && inc>0));
for (i=1; i<250; i++) {
_root["star_mc"+i]._y += inc;
if (_root["star_mc"+i]._y>=400) {
_root["star_mc"+i]._y = random(400)*-1;
}
if (Key.isDown(Key.ENTER)) {
_root["star_mc"+i]._x = random(550);
_root["star_mc"+i]._y = random(400);
}
}
};
it works exactly the same so yay! ^_^.
also could you pot your tutorial on the burstfilm tutorial database it'd be much appreciated

Some times my "L" key decides not to work.


None

DomBern

Reply To Post Reply & Quote

Posted at: 7/27/05 05:30 AM

DomBern EVIL LEVEL 07

Sign-Up: 10/09/04

Posts: 83

I put that into one of my movies and it covered everything up. Help?


None

3ruce

Reply To Post Reply & Quote

Posted at: 7/27/05 05:36 AM

3ruce LIGHT LEVEL 09

Sign-Up: 07/23/05

Posts: 1,012

At 2/13/05 07:02 PM, RuneAura wrote: You have considered making a tutorial right?

One of the things I tried to teach myself (and after much time did) was how to draw in actionscript. You could easily make one of these and I think everyone whould be interested on how to do it.

Well anyways that's just one guy's opinion. If you do newgroundites will love you. Of course why not let 'em suffer like me while i tried to figure out how to do it?

Dude please tell me thats not a siggy for runescape... if it is *tears guys balls out of scrotum!!!*

Murad136, heres your fucking credit ;3

BBS Signature

None

DomBern

Reply To Post Reply & Quote

Posted at: 7/27/05 05:40 AM

DomBern EVIL LEVEL 07

Sign-Up: 10/09/04

Posts: 83

Dude I'm making a game and if you could make it so that the stars where under the earth/space craft etc. that would help


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 7/27/05 05:59 AM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

just put the depth of the space craft higher than the depth of the stars(301+)

Some times my "L" key decides not to work.


Happy

Steadyeddy

Reply To Post Reply & Quote

Posted at: 7/27/05 05:04 PM

Steadyeddy NEUTRAL LEVEL 04

Sign-Up: 01/07/05

Posts: 16

Hey if you put the random terrain script with the starfield script you get snow


None

Rantzien

Reply To Post Reply & Quote

Posted at: 7/27/05 05:09 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

A problem is that speeding it up screws up the whole starfield.

BBS Signature

None

Rantzien

Reply To Post Reply & Quote

Posted at: 7/27/05 05:11 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

At 7/27/05 05:09 PM, Rantzien wrote: A problem is that speeding it up screws up the whole starfield.

Oh, sorry. Guess I should've read the whole thread before I posted.

BBS Signature

None

Rantzien

Reply To Post Reply & Quote

Posted at: 7/27/05 05:13 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

At 7/27/05 05:40 AM, DomBern wrote: Dude I'm making a game and if you could make it so that the stars where under the earth/space craft etc. that would help

Just increase the depth of your earth/space craft.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 04:11 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!