Spider Web

I was looking at a spider web the other day, and in typical Flash geek fashion, thought to myself, “I could do that in Flash!”

So I did.

Take that, spiders! A million years of evolution, and I can do it in 15 minutes – on the timeline no less!

[kml_flashembed movie=”http://www.bit-101.com/misc/web01.swf” width=”550″ height=”400″/]

Click to generate a web. Click again to generate a new one. No spiders required.

[as]var center:Point;
var radius:Number;
var increase:Number = 1;
var numSpokes:uint;
var spokes:Array;
var spokeIndex:uint;

stage.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent):void
{
center = new Point(mouseX, mouseY);
radius = 1;
numSpokes = Math.floor(Math.random() * 5 + 10);
spokes = new Array();
spokeIndex = 0;
graphics.clear();
graphics.lineStyle(0, 0xffffff, .3);
removeEventListener(Event.ENTER_FRAME, draw);
addEventListener(Event.ENTER_FRAME, drawSpokes);
}

function drawSpokes(event:Event):void
{
if(spokeIndex >= numSpokes)
{
removeEventListener(Event.ENTER_FRAME, drawSpokes);
addEventListener(Event.ENTER_FRAME, draw);
graphics.moveTo(center.x, center.y);
spokeIndex = 0;
}
else
{
var spoke:Number = Math.PI * 2 / numSpokes * spokeIndex + Math.random() * .4 – .2;
spokes.push(spoke);
graphics.moveTo(center.x, center.y);
graphics.lineTo(center.x + Math.cos(spoke) * 1000, center.y + Math.sin(spoke) * 1000);
spokeIndex++;
}
}

function draw(event:Event):void
{
var spoke:Number = spokes[spokeIndex];
var r:Number = radius + Math.random() * 8 – 4;
var xpos:Number = center.x + Math.cos(spoke) * r;
var ypos:Number = center.y + Math.sin(spoke) * r;
graphics.lineTo(xpos, ypos);
radius += increase;
spokeIndex++;
if(spokeIndex >= spokes.length) spokeIndex = 0;
if(radius > stage.stageWidth / 2)
{
removeEventListener(Event.ENTER_FRAME, draw);
}
}[/as]

This entry was posted in Flash. Bookmark the permalink.

9 Responses to Spider Web

  1. Patrick Tai says:

    … psst background color is the same as drawing lines 😉

  2. kp says:

    well obviously you’ll want to change the background color to something different.

  3. Gilbert says:

    Keith,

    Yeap, you can do in 15 minutes, but yours is not sticky and don’t capture any flies:)

    Nice work.

  4. kp says:

    Well who the hell wants to catch flies? Yuck! 😉

  5. So, flies generator is needed, then some wind, maybe some gravity, then Papervision3D and APE integration and then – meet Web 3D game on your screen!

    Also, your app could be a good screen saver.

  6. Hold back on the revisions until Halloween. Timing in marketing is everything. After the web is spun, have a big spider crawl around and its head can be a photo upload of your choice. The kids will love it.

  7. Rick says:

    cool graphic. I attached it to a button as a rollover but how do I code it to stop and remove it from the stage?

Leave a Reply