Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > Community Boards > Just for Kicks Challenges

Reply
 
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
Old 03-14-2007, 03:29 PM   #1
pixelwit
village halfwit
 
pixelwit's Avatar
 
Join Date: Jul 2001
Location: USA, PA
Posts: 3,187
Default 256 Byte Starfield

You may have seen a 256 byte JavaScript StarField effect being discussed on Digg .

When I first saw it, I was certain I could achieve an equally small and nearly identical effect with ActionScript, but now I'm not so sure. There are 3 parts to this challenge.

First, replicate the effect with ActionScript alone, this is good practice for the newbies.

Second, condense the text used in your code to a "TXT" or "AS" file no greater than 256 bytes in size.

Third, compile your script to a SWF file smaller than 256 bytes.

As an example, the following code compiles to a 376 byte SWF file (use 550x400, black background and 32 FPS).
ActionScript Code:
// i=0; _x=275; _y=200; m=Math; onEnterFrame=function(){     c=createEmptyMovieClip("c"+i,++i);     c.lineStyle(2,0xFFFFFF);     c.lineTo(1,0);     c._alpha=0;     c.d=1;     c.r=m.random()*m.PI*2;     c.onEnterFrame=function(){         with(this){             d*=1.1;             if(d>300)removeMovieClip();             _alpha=d/2;             _x=m.cos(r)*d;             _y=m.sin(r)*d;         }     } } //

The above code with white space removed looks like the following and creates a 273 byte TXT or AS file:
Code:
i=0;_x=275;_y=200;m=Math;onEnterFrame=function(){c=createEmptyMovieClip("c"+i,++i);c.lineStyle(2,0xFFFFFF);c.lineTo(1,0);c._alpha=0;c.d=1;c.r=m.random()*m.PI*2;c.onEnterFrame=function(){with(this){d*=1.1;if(d>300)removeMovieClip();_alpha=d/2;_x=m.cos(r)*d;_y=m.sin(r)*d;}}}
Do you think you can trim 17 bytes off the above code? Maybe you can come up with an entirely different way to code a starfield?

Good luck and happy Flashing.

-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.

Last edited by pixelwit; 03-14-2007 at 06:10 PM..
pixelwit is offline   Reply With Quote
Old 03-15-2007, 01:40 AM   #2
darkzak
Registered User
 
Join Date: Apr 2003
Location: Salinas, California
Posts: 210
Default

Interesting challenge

First of all here is your code shortened to 236 bytes (when whitespace is removed):
ActionScript Code:
i=0; _y=_x=200; m=Math; onEnterFrame=function(){            c=createEmptyMovieClip(i,++i%300);      c.lineStyle(2,0xFFFFFF);     c.lineTo(1,0);     c._alpha=0;     c.d=1;     c.r=m.random()*6;     c.onEnterFrame=function(){         with(this){             d*=1.1;             _alpha=d/2;             _x=m.cos(r)*d;             _y=m.sin(r)*d         }     } }

and the compressed version:
ActionScript Code:
i=0;_y=_x=200;m=Math;onEnterFrame=function(){c=createEmptyMovieClip(i,++i%300);c.lineStyle(2,0xFFFFFF);c.lineTo(1,0);c._alpha=0;c.d=1;c.r=m.random()*6;c.onEnterFrame=function(){with(this){d*=1.1;_alpha=d/2;_x=m.cos(r)*d;_y=m.sin(r)*d}}}

The .swf version is now 344 bytes.

Here are the changes I made:
1. Most of the bytes were saved by not removing the movieclips when d > 300. Instead the movieclips are written over by creating new mcs at the same depth.

2. set root _y = _x instead of different values.

3. Instead of using Math.PI*2 just rounded to 6
darkzak is offline   Reply With Quote
Old 03-15-2007, 02:05 AM   #3
pixelwit
village halfwit
 
pixelwit's Avatar
 
Join Date: Jul 2001
Location: USA, PA
Posts: 3,187
Default

Nice.

Overwriting the clips sounds like a good idea, as long as it doesn't create a memory issues I'm all for it. I really like the ++% bit. I've always been a fan of both ++ and % but never thought to combine them as such.

I considered setting _x and _y to the same coordinates but a square screen varies a bit from the JavaScript original. I'd hoped to post a link directly to a SWF file (not embedded in a HTML page) which would load in the browser and replicate the JS effect as closely as possible. I'm willing to make concessions though.

6 is nowhere near 6.28318530717959! Good thinking. If you're going to use an integer, you could get away with using random(7) rather than m.random()*6. (EDIT: Oops, never mind, that only returns integers.)

Looks like getting a 256B SWF is going to be a real tough one to crack.

Thanks for taking up the challenge.

-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.

Last edited by pixelwit; 03-15-2007 at 02:12 AM..
pixelwit is offline   Reply With Quote
Old 04-02-2007, 08:43 PM   #4
iamp01
Registered User
 
Join Date: Apr 2007
Posts: 2
Default

Hello,

I wrote the above ( or is it below ? ) mentionned 256b 3D Starfield in JavaScript.

It's nice to see some ActionScript'ers giving a try at hardcore size optimization.

The only ActionScript I made was a tiny ( ~170bytes ) mp3 player for JavaScript were I passed the URL of the music in GET and had a method to start/stop the replay once a variable exposed to JavaSscript was set. All this was done using a Flash 8 assembler/disassembler. Is there any more practical tool ... and free. ActionScript 3 look really interresting.

If ever 256b is not challenging enough, I shrinked the 3D Starfield effect to 209 bytes ( and even 207b but it doesn't look as good ), and also made a 256b Mandelbrot rotozoom in JavaScript.


Happy optimization
iamp01 is offline   Reply With Quote
Old 04-06-2007, 05:14 PM   #5
darkzak
Registered User
 
Join Date: Apr 2003
Location: Salinas, California
Posts: 210
Default

Quote:
Originally Posted by iamp01 View Post
Is there any more practical tool ... and free. ActionScript 3 look really interresting.

I use Flashdevelop for Actionscript projects, it can be setup to compile AS3:
http://www.flashdevelop.org/community/

I think the flash code for just opening a movie is too much overhead to make a 256byte swf but it is a challenge trying.
darkzak is offline   Reply With Quote
Old 04-06-2007, 06:02 PM   #6
iamp01
Registered User
 
Join Date: Apr 2007
Posts: 2
Default

Thanks for hint.

There's a couple of Flash entries in the 256b.htm contests. But that was probably Flash 7 or less at the time.
iamp01 is offline   Reply With Quote
Old 04-09-2007, 09:59 AM   #7
monsieurfil
Registered User
 
monsieurfil's Avatar
 
Join Date: Jun 2005
Posts: 20
Default

160 bytes, using the same math trick as in the JS starfield
Code:
_x=_y=k=200
c=Math.cos
onEnterFrame=function(){
	clear()
	for(i=0;i<65;){
		z=2000/(73-(++i+k++&63))
		lineStyle(2,-1,z-20)
		moveTo(x=z*c(i*.9),y=z*c(i))
		lineTo(x+1,y)
	}
}
monsieurfil is offline   Reply With Quote
Old 04-11-2007, 12:57 AM   #8
pixelwit
village halfwit
 
pixelwit's Avatar
 
Join Date: Jul 2001
Location: USA, PA
Posts: 3,187
Default

Hey, it's good to see there's still some interest in this.

Hi P01, glad you could stop by to see the challenge which you helped inspire. The ASCII Mandelbrot is pretty neat too.

Nice adaptation of the JS code Monsieurfil. I might have tried the same but I wasn't comfortable using code I didn't fully understand. I'm guessing 63 is related to PI, but other than that I'm at a loss.

I'm not sure if anyone will be able to get the SWF file size down to 256 bytes, but Monsieurfil's code compiles to a 315 byte SWF and that's as close as anyone has come so far.

-PiXELWIT
http://www.pixelwit.com
__________________
There are no answers, only choices.
pixelwit is offline   Reply With Quote
Old 04-14-2007, 12:40 PM   #9
monsieurfil
Registered User
 
monsieurfil's Avatar
 
Join Date: Jun 2005
Posts: 20
Default

The JS code is a pretty clever mathematical trick wich has nothing to do with PI (and I can't pretend I got it fully).

I think that getting under 256bytes will require to manually optimize the SWF binary - I'll try that
monsieurfil is offline   Reply With Quote
Old 04-17-2007, 08:49 AM   #10
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 838
Default

Ah interesting.....never saw something written like that.....

the &63 in (++i+k++&63) is a bitwise AND evaluation between the left side and 63. The significance of 63 is that its one less than 64 (2^6). This becomes 111111 in binary...which means it always satisfies the evaluation of the first 6 binary digits, and none after that. Basically this results in a loop effect of the value being returned.

It could have just as easily been written as (++i+k++%64)
Assertnfailure is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Starfield Class MichaelxxOA ActionScript 2.0 8 05-26-2006 08:35 PM
stop starfield? Bsouth ActionScript 1.0 (and below) 2 02-13-2006 05:13 PM
Modified starfield effect. (Changing many movieclips after creation) cmh84 ActionScript 2.0 4 03-09-2005 03:38 AM
Sending 1 byte data over 127?! anarkizt ActionScript 2.0 2 12-27-2004 08:27 AM
That dreaded null byte again apesaga Server-Side Scripting 0 12-16-2004 12:14 PM


All times are GMT. The time now is 01:24 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.
You Rated this Thread: