00:00
00:00
Newgrounds Background Image Theme

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

AS: Random

9,439 Views | 32 Replies
New Topic Respond to this Topic

AS: Random 2005-02-11 18:12:58


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.

Response to AS: Random 2005-02-11 18:18:24


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

Response to AS: Random 2005-02-11 19:00:27


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.

};

Response to AS: Random 2005-02-11 20:06:11


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

Cheers man, it's a lot clearer now.


- - Flash - Music - Images - -

BBS Signature

Response to AS: Random 2005-02-11 23:12:28


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

Response to AS: Random 2005-02-11 23:23:03


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

Response to AS: Random 2005-02-12 15:25:20


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.

Response to AS: Random 2005-02-12 17:23:04


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!


Sup, bitches :)

BBS Signature

Response to AS: Random 2005-02-12 17:24:26


Noooooo! Not the green box! My presssssssious!

Response to AS: Random 2005-02-12 17:27:20


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.


Sup, bitches :)

BBS Signature

Response to AS: Random 2005-02-12 17:30:19


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

Response to AS: Random 2005-02-13 08:20:31


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


Sup, bitches :)

BBS Signature

Response to AS: Random 2005-02-13 16:56:18


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();
}

Response to AS: Random 2005-02-13 17:30:28


Here's a web version:

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

Press enter to reset terrain.

Response to AS: Random 2005-02-13 19:02:10


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."

Response to AS: Random 2005-02-13 19:41:54


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.

Response to AS: Random 2005-02-14 17:49:01


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.

Response to AS: Random 2005-03-12 18:19:03


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;
};

Response to AS: Random 2005-06-28 09:13:43


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

Response to AS: Random 2005-07-27 04:29:06


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():

Response to AS: Random 2005-07-27 05:01:49


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

Response to AS: Random 2005-07-27 05:30:47


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

Response to AS: Random 2005-07-27 05:36:13


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

Response to AS: Random 2005-07-27 05:40:03


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

Response to AS: Random 2005-07-27 05:59:33


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

Response to AS: Random 2005-07-27 17:04:31


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

Response to AS: Random 2005-07-27 17:09:45


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


BBS Signature

Response to AS: Random 2005-07-27 17:11:23


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

Response to AS: Random 2005-07-27 17:13:56


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

Response to AS: Random 2005-08-06 06:45:23


Does anyone know how to generate random movement/ spawning of objects in flash?
I've looked for tutorials on the subject but they don't make any sense.