Be a Supporter!

AS: Starfield Script

  • 4,361 Views
  • 30 Replies
New Topic Respond to this Topic
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
AS: Starfield Script 2005-07-23 07:41:36 Reply

AS: Main

Starfield

This code uses the following:
Intervals, Functions, attachMovie, Variables, Maths, Conditions, Movement, Arrays, and a touch of API.

====================== ======================

This is a simple star field, created dynamically. Add this code to the first frame of your movie:

//Black background, 20+ FPS
var frequency=50; //new stars per second
var speed=5; //star speed
var accel=20; //star acceleration

SW=Stage.width; SH=Stage.height;
c=1000;

//Create timer function
interv = setInterval(function () {
makestar()
}, 1000/frequency);

//Function to create a new star
function makestar(){
c++; //Use for unique Instance Name and depth
if(c>2000){ c=1000; }

//MAKE NEW STAR
_root.createEmptyMovieClip("star"+c, c)
with(_root["star"+c]){
//Draw a circle (with API)
lineStyle(2, 0xFFFFFF, 100);
lineTo(0.2, 0);
//Set position, direction and alpha
_x=SW/2; _y=SH/2;
_alpha=0;
_rotation=random(360);
}
//Set speed
_root["star"+c].spd=random(speed/2)+speed/
2;

//TELL NEW STAR WHAT TO DO EACH FRAME
_root["star"+c].onEnterFrame= function(){
with(this){
//Speed it up, make it bigger
spd*=1+(_root.accel/500);
_width*= 1+(_root.accel/2500);
_height*= 1+(_root.accel/2500);
_alpha+=3;
//Move it
if(_rotation>180){
_y+=(spd*Math.cos(Math.PI/180*_rotation));
_x-=(spd*Math.sin(Math.PI/180*_rotation));
}else{
_y-=(spd*Math.cos(Math.PI/180*_rotation));
_x+=(spd*Math.sin(Math.PI/180*_rotation));
}
//If off-screen, remove star
if(_x<0-_width || _x>SW+_width || _y>SH+_width ||_y<-_width){
this.removeMovieClip();
}
}
}
}

RESULT

====================== ======================

This is virtually the same code, but uses an attached movie rather than an API dot as the star. For this to work, you'll need to:

Right-click the MC in the library.
Select 'Linkage'
Tick the 'Export for Actionscript' box
Give it the Identifier 'star'

Add this code to the first frame of your movie:

//Black background, 20+ FPS
var frequency=50; //new stars per second
var speed=5; //star speed
var accel=20; //star acceleration

SW=Stage.width; SH=Stage.height;
c=1000;

//Create timer function
interv = setInterval(function () {
makestar()
}, 1000/frequency);

//Function to create a new star
function makestar(){
c++; //Use for unique Instance Name and depth
if(c>2000){ c=1000; }

//MAKE NEW STAR
_root.attachMovie("star", "star"+c, c)
with(_root["star"+c]){
//Set position, direction and alpha
_x=SW/2; _y=SH/2;
_alpha=0;
_rotation=random(360);
}
//Set speed
_root["star"+c].spd=random(speed/2)+speed/
2;

//TELL NEW STAR WHAT TO DO EACH FRAME
_root["star"+c].onEnterFrame= function(){
with(this){
//Speed it up, make it bigger
spd*=1+(_root.accel/500);
_width*= 1+(_root.accel/2500);
_height*= 1+(_root.accel/2500);
_alpha+=3;
//Move it
if(_rotation>180){
_y+=(spd*Math.cos(Math.PI/180*_rotation));
_x-=(spd*Math.sin(Math.PI/180*_rotation));
}else{
_y-=(spd*Math.cos(Math.PI/180*_rotation));
_x+=(spd*Math.sin(Math.PI/180*_rotation));
}
//If off-screen, remove star
if(_x<0-_width || _x>SW+_width || _y>SH+_width ||_y<-_width){
this.removeMovieClip();
}
}
}
}

RESULT (MC was a light blue line)

====================== ======================

WHAT!?

Both of these codes basically do the same thing: they create a new star at the centre of the stage every X milliseconds, based upon the interval set.
As it is initiated, each star is assigned position, speed, alpha and rotation values.
Then, on each new frame, the star's values for alpha, size and speed are adjusted. The star is then moved based upon these settings.
Finally, when the star is out of sight, it is removed.

====================== ======================

Some interesting variations:

To spin yourself out, find this line in the code:
_alpha+=3;
And add this after it:
_rotation+=5;
RESULT

To make the 'source spot' follow the mouse, replace this line:
_x=SW/2; _y=SH/2;
with these:
_x= _root._xmouse;
_y= _root._ymouse

This looks better if you change this line:
_alpha=0;
to
_alpha=50;
RESULT

For multicoloured stars, use the first (API dot) code, and add this at the top:
cols=new Array(0xFF0000, 0xFF9900, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF, 0xFF66FF, 0xCC33FF)
Then change this line:
lineStyle(2, 0xFFFFFF, 100);
to this:
lineStyle(2, cols[random(cols.length)], 100);
RESULT

====================== ======================

Basically, play with bits of the code to see what happens when you adjust values. This is quite a good sample for trying out various different aspects of AS. Have fun =)


- - Flash - Music - Images - -

BBS Signature
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Starfield Script 2005-07-23 07:45:45 Reply

At 7/23/05 07:41 AM, Denvish wrote: To make the 'source spot' follow the mouse, replace this line:
_x=SW/2; _y=SH/2;
with these:
_x= _root._xmouse;
_y= _root._ymouse
This looks better if you change this line:
_alpha=0;
to
_alpha=50;
RESULT

Wow, that's a really sweet code.. good job.


Sup, bitches :)

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Starfield Script 2005-07-23 07:51:18 Reply

actually, I would have scripted it excactly the same way ;)

Very nice job denvish

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Starfield Script 2005-07-23 08:00:48 Reply

Hmmm, bloody BBS auto-linebreak fucks things up again.
This line in both codes (at the end of the MAKE NEW STAR section):

_root["star"+c].spd=random(speed/2)+speed/
2;

should be:

_root["star"+c].spd= random(speed/2)+speed/2;

if you get errors when copy/pasting, that'll probably be why


- - Flash - Music - Images - -

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Starfield Script 2005-07-23 08:09:10 Reply

I am teh 1337 modder!

add this to the end of the original code

this.onEnterFrame = function() {
_root.createEmptyMovieClip("starshape", 5020);
_root.starshape.lineStyle(3, 0xFFFFFF, 20);
startc = c-20;
lol = c-21;
_root.starshape.moveTo(_root["star"+lol]._
x, _root["star"+lol]._y);
for (i=startc; i<c; i++) {
_root.starshape.lineTo(_root["star"+i]._x, _root["star"+i]._y);
trace("line to"+_root["star"+i]._x+","+_root["star"+i]
._y);
}
};

example:
http://img301.images..hp?image=gggg2lv.swf

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Starfield Script 2005-07-23 08:11:48 Reply

At 7/23/05 08:09 AM, Inglor wrote: I am teh 1337 modder!
lol = c-21;
http://img301.images..hp?image=gggg2lv.swf

Heh, that's pretty sweet. I particularly like the variable 'lol'


- - Flash - Music - Images - -

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Starfield Script 2005-07-23 08:16:12 Reply

this.onEnterFrame = function() {
_root.createEmptyMovieClip("starshape", 5020);
_root.starshape.lineStyle(3, 0xFFFFFF, 20);
startc = c-20;
lol = c-21;
_root.starshape.moveTo(_root["star"+lol]._
x, _root["star"+lol]._y);
for (i=startc; i<c; i++) {
_root.starshape.curveTo(_root["star"+i]._x
, _root["star"+i]._y,SW/2,SH/2);
trace("line to"+_root["star"+i]._x+","+_root["star"+i]
._y);
}
};

altered it a bit, looks nice

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Starfield Script 2005-07-23 08:50:36 Reply

I made it customizable :P

http://img311.images..ablestarfield3ep.swf

Just select one of the shapes and that will be the star.


Sup, bitches :)

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Starfield Script 2005-07-23 08:51:48 Reply

you didn't add my extra to the options :'(

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Starfield Script 2005-07-23 08:59:07 Reply

At 7/23/05 08:51 AM, Inglor wrote: you didn't add my extra to the options :'(

Just seen it, pretty cool :P


Sup, bitches :)

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Starfield Script 2005-07-23 09:06:30 Reply

still no denvish or inglor

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Starfield Script 2005-07-23 09:28:51 Reply

At 7/23/05 08:11 AM, Denvish wrote:
At 7/23/05 08:09 AM, Inglor wrote: lol = c-21;

Hey! You're teh copier!
I started typin those variables in my AS thread >: (

Shame on YOU.

:P


BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Starfield Script 2005-07-23 09:31:15 Reply

no way, I used funky vars before flash was invented :P

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Starfield Script 2005-07-23 09:48:00 Reply

At 7/23/05 09:31 AM, Inglor wrote: no way, I used funky vars before flash was invented :P

It's true, Inglors variables are always funky. 1337, lol, etc.


Sup, bitches :)

BBS Signature
sysrq868
sysrq868
  • Member since: Feb. 9, 2005
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to AS: Starfield Script 2005-07-23 10:21:56 Reply

That looks pretty sweet... Better than I expected...


I'm built from the leftover parts.

Chris
Chris
  • Member since: Sep. 17, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Starfield Script 2005-11-21 10:52:20 Reply

soeey if i missed it, but how do i get the star field to stop?

Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Starfield Script 2005-11-21 11:05:20 Reply

At 11/21/05 10:52 AM, chrisexe wrote: soeey if i missed it, but how do i get the star field to stop?

This will stop it:
MovieClip.prototype.spd = 0;

BUT that means you won't be able to start it again, at least not with the same speed as before. So instead, you can modify this piece of code:

_root["star"+c].onEnterFrame= function(){
with(this){
//Speed it up, make it bigger
spd*=1+(_root.accel/500);
_width*= 1+(_root.accel/2500);
_height*= 1+(_root.accel/2500);
_alpha+=3;
//Move it
if(_rotation>180){
_y+=(spd*Math.cos(Math.PI/180*_rotation));
_x-=(spd*Math.sin(Math.PI/180*_rotation));
}else{
_y-=(spd*Math.cos(Math.PI/180*_rotation));
_x+=(spd*Math.sin(Math.PI/180*_rotation));
}
//If off-screen, remove star
if(_x<0-_width || _x>SW+_width || _y>SH+_width ||_y<-_width){
this.removeMovieClip();
}
}
}
}

To:

_root["star"+c].onEnterFrame= function(){
if (!_root.paused) {
with(this){
//Speed it up, make it bigger
spd*=1+(_root.accel/500);
_width*= 1+(_root.accel/2500);
_height*= 1+(_root.accel/2500);
_alpha+=3;
//Move it
if(_rotation>180){
_y+=(spd*Math.cos(Math.PI/180*_rotation));
_x-=(spd*Math.sin(Math.PI/180*_rotation));
}else{
_y-=(spd*Math.cos(Math.PI/180*_rotation));
_x+=(spd*Math.sin(Math.PI/180*_rotation));
}
//If off-screen, remove star
if(_x<0-_width || _x>SW+_width || _y>SH+_width ||_y<-_width){
this.removeMovieClip();
}
}
}
}
};

Then you can stop it with:

_root.paused = true;

And start it again with:

_root.paused = false;


BBS Signature
Creepy
Creepy
  • Member since: Nov. 28, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Starfield Script 2005-11-21 11:10:14 Reply

At 11/21/05 10:52 AM, chrisexe wrote: soeey if i missed it, but how do i get the star field to stop?

makestar = null;

IWantSomeCookies
IWantSomeCookies
  • Member since: Aug. 20, 2004
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to AS: Starfield Script 2005-12-16 05:49:04 Reply

Wow this is an awesome code Denvish. I will try and play with it to see what I come up with, haha.


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

frodo1090
frodo1090
  • Member since: Feb. 19, 2005
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS: Starfield Script 2005-12-16 06:06:10 Reply

it this shit got sample...?what should it look like.... man be serious.

IWantSomeCookies
IWantSomeCookies
  • Member since: Aug. 20, 2004
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to AS: Starfield Script 2005-12-16 07:00:26 Reply

At 12/16/05 06:06 AM, frodo1090 wrote: it this shit got sample...?what should it look like.... man be serious.

Huh? He posted RESULT everywhere..

This is the orginal version of the script.


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

Ssilver7
Ssilver7
  • Member since: Feb. 19, 2004
  • Offline.
Forum Stats
Member
Level 35
Blank Slate
Response to AS: Starfield Script 2005-12-16 07:23:23 Reply

Whoah, pretty cool stuff, makes me want to try my hand at AS again.


BBS Signature
sysrq868
sysrq868
  • Member since: Feb. 9, 2005
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to AS: Starfield Script 2006-01-09 09:27:34 Reply

Bumpy, bumpy!

I noticed that these stars, whatever might they be, are on top of everything, even if they are on a lower layer. Is there a way to put them behind everything?

Plus, how can I make random movieclips as stars... like the multicolor-star mod, but with movieclips?


I'm built from the leftover parts.

Twist-Chao06
Twist-Chao06
  • Member since: Jan. 21, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Starfield Script 2007-04-23 12:21:38 Reply

Im Sorry, But You said The Script to Stop the Star Field, But i Need one to Completly Stop and Remove it from a Frame. because if i put it on one frame, it continues to go on the Next Frame: How would i stop that from Happening?

Twist-Chao06
Twist-Chao06
  • Member since: Jan. 21, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Starfield Script 2007-04-23 12:25:32 Reply

Ahhh, Just Got It

Fickludd
Fickludd
  • Member since: Feb. 28, 2004
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Starfield Script 2007-08-09 07:30:15 Reply

Thanks for the script Denvish :D.

I modded it heavily to particle effect snow for Artifission Ch. 1, here's the code if anyone needs snow

It uses depths 500-800 in _root

var frequency:Number = 50; //new stars per second
var speed:Number = 2; //star speed
var accel:Number = 10; //star acceleration
var Bord:Number = 100; //How much outside the visible that will be snowFilled

var SW:Number = Stage.width + Bord;
var SH:Number = Stage.height + Bord;
var c:Number = 500;

//Function that's starts the snowing
function startSnow() {
snowInterval = setInterval(function() {
makeFlake()
}, 1000/frequency);
}
//Function to create a new star
function makeFlake() {
c++; //Use for unique Instance Name and depth
if (c > 800) {
c = 500;
}

//MAKE NEW FLAKE
_root.createEmptyMovieClip("flake"+c, c)
with(_root["flake"+c]) {
//Draw a circle (with API)
lineStyle(20, 0xFFFFFF, 100);
lineTo(1, 0);
//Set position, direction
_x = Math.floor(Math.random()*SW) - _root.Bord/2 + _root.vCam_mc._x - _root.vCam_mc._width/2;
_y = Math.floor(Math.random()*SH) - _root.Bord/2 + _root.vCam_mc._y - _root.vCam_mc._height/2;
_alpha = 0;
//_rotation = Math.floor(Math.random()*360);
}
//Set speed
_root["flake"+c].spd = Math.random()*speed/2 + speed/2;

//TELL NEW FLAKE WHAT TO DO EACH FRAME
_root["flake"+c].onEnterFrame = function() {
with(this) {
//Slow it down, make it smaller
spd /= (1 + (_root.accel/1000));//*(1 + (_root.accel/1000));
_width /= (1 + (_root.accel/500))*(1 + (_root.accel/500));
_height /= (1 + (_root.accel/500))*(1 + (_root.accel/500));
_alpha += 5;
//Move it
_y += spd;

//If off-screen, remove star
if (_x < -_width - _root.Bord/2 + _root.vCam_mc._x - _root.vCam_mc._width/2 ||
_x > _width + SW - _root.Bord/2 + _root.vCam_mc._x - _root.vCam_mc._width/2 ||
_y > _width + SH - _root.Bord/2 + _root.vCam_mc._y - _root.vCam_mc._height/2 ||
_y < -_width - _root.Bord/2 + _root.vCam_mc._y - _root.vCam_mc._height/2 ||
spd < 0.1) {
this.removeMovieClip();
}
}
}
}
//Function that stops the snowing and removes all flakes
function clearSnow() {
clearInterval(snowInterval);
for (i=500; i<801; i++) {
_root["flake"+i].removeMovieClip();
}
}

Happy X-mas or whatever.

Fickludd
Fickludd
  • Member since: Feb. 28, 2004
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Starfield Script 2007-08-09 07:32:15 Reply

Oh, and it's scripted to follow my vCam named vCam_mc ^^.

wolfinator-x
wolfinator-x
  • Member since: Jul. 17, 2006
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to AS: Starfield Script 2007-09-02 15:54:42 Reply

http://www.ngup.net/view.php?file=star

Transparent ship, yes I know but let's just say What The Fuck.


BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Starfield Script 2007-09-02 16:03:29 Reply

At 9/2/07 03:54 PM, wolfinator-x wrote: http://www.ngup.net/view.php?file=star

Transparent ship, yes I know but let's just say What The Fuck.

onClipEvent(load){this.swapDepths(5000);
}


- - Flash - Music - Images - -

BBS Signature
zenyara
zenyara
  • Member since: Jun. 17, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: Starfield Script 2007-09-02 16:15:37 Reply

Wow, this would be great for particle effects (explosions and bloodsplatter).