PDA

View Full Version : Pulsating dynamically drawn object


suburbiaboy
10-26-2009, 04:49 PM
I'm using the following script to create a falling "bokeh" effect and I'm trying to make some specific modifications: Firstly I want the circles to pulse (slowly randomly fade alpha in and out): Secondly it would be better if the circles were hexagonal.

I've been playing with fl.transitions.Tween and fl.transitions.Fade, which I had a little luck with but I'm completely new to as3 and strugling to get it working fully.

Many thanks to anyone who has a chance to look at this.

Jamie

Also the script is from a tutorial as you may notice.

/* Blurry Lights Animation */
/* Developed by Carlos Yanez */

package Classes
{
/* Import required classes */

import flash.display.MovieClip;
import flash.filters.BitmapFilter;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.events.Event;

public class Lights extends MovieClip
{
/* Vars */
private var lightsNumber:int;
private var xSpeed:int;
private var ySpeed:int;
private var lightsDir:String;
private var areaW:int;
private var areaH:int;
private var lights:Array = new Array();

/* Main function */

public function init(areaWidth:int, areaHeight:int, numberOfLights:int, lightSize:int, lightColor:uint, minimumScale:Number, hSpeed:int, vSpeed:int, dir:String, quality:int):void
{

/* Set variables */

areaW = areaWidth;
areaH = areaHeight;
lightsNumber = numberOfLights;
lightsDir = dir;

/* Use a for to create the parameter-specified number of lights*/

for (var i:int = 0; i < numberOfLights; i++)
{
/* Create the specified number of lights */
var light:MovieClip = new MovieClip();
/* Set random speed to x and y based on the params*/
xSpeed = Math.floor((Math.random() * (hSpeed - -hSpeed + 1)) + -hSpeed);
ySpeed = Math.round((Math.random() * vSpeed) + 0.5);
light.xSpeed = xSpeed;
light.ySpeed = ySpeed;
/* Create lights */
light.graphics.beginFill(lightColor);
light.graphics.drawCircle(0, 0, lightSize / 2);
light.graphics.endFill();
/* Set a position based on the params specified */
light.x = Math.floor(Math.random() * areaWidth);
light.y = Math.floor(Math.random() * areaHeight);
/* Add blur, we declare the var here to get a new blur w/every light */
var b:int = Math.floor(Math.random() * 10) + 2;
var blur:BitmapFilter = new BlurFilter(b,b,quality);
//
var glow:GlowFilter = new GlowFilter;
glow.color = 0xE2FFFF;
glow.blurX = 10;
glow.blurY = 10;
glow.inner = true;
var filterArray:Array = new Array(blur, glow);
light.filters = filterArray;
//
/* Change alpha */
light.alpha = Math.random() * 0.6 + 0.1;
/* Scale */
light.scaleX = Math.round(((Math.random() * (1 - minimumScale)) + minimumScale) * 100) / 100;
light.scaleY = light.scaleX;
/* Add the lights */
addChild(light);
/* Store lights in an array to use it later */
lights.push(light);
/* Check for lights direction */
checkDirection();
}
}

/* Check direction */

private function checkDirection():void
{
for (var i:int = 0; i < lights.length; i++)
{
switch ( lightsDir )
{
case "up" :
lights[i].addEventListener(Event.ENTER_FRAME, moveUp);
break;
case "down" :
lights[i].addEventListener(Event.ENTER_FRAME, moveDown);
break;
case "right" :
lights[i].addEventListener(Event.ENTER_FRAME, moveRight);
break;
case "left" :
lights[i].addEventListener(Event.ENTER_FRAME, moveLeft);
break;
default :
trace("Something weird just happened!");
}
}
}

/* Move Up function */

private function moveUp(e:Event):void
{
e.target.x += e.target.xSpeed;
e.target.y-=e.target.ySpeed;
/* Reset light position, Y first, then X */
if (e.target.y + (e.target.height / 2) < 0)
{
e.target.y = areaH + (e.target.height / 2);
e.target.x=Math.floor(Math.random()*areaW);
}
if ((e.target.x + e.target.width / 2) < 0 || (e.target.x - e.target.width / 2) > areaW)
{
e.target.y = areaH + (e.target.height / 2);
e.target.x=Math.floor(Math.random()*areaW);
}
}

/* Move Down function */

private function moveDown(e:Event):void
{
e.target.x+=e.target.xSpeed;
e.target.y+=e.target.ySpeed;
/* Reset light position, Y first, then X */
if (e.target.y - (e.target.height / 2) > areaH)
{
e.target.y = 0 - (e.target.height / 2);
e.target.x=Math.floor(Math.random()*areaW);
}
if ((e.target.x + e.target.width / 2) < 0 || (e.target.x - e.target.width / 2) > areaW)
{
e.target.y = areaH + (e.target.height / 2);
e.target.x=Math.floor(Math.random()*areaW);
}
}

/* Move Right function */

private function moveRight(e:Event):void
{
e.target.x+=e.target.ySpeed;
e.target.y+=e.target.xSpeed;
/* Reset light position, Y first, then X */
if (e.target.y - (e.target.height / 2) > areaH || e.target.y + (e.target.height / 2) < 0)
{
e.target.x = 0 - (e.target.height / 2);
e.target.y = Math.floor(Math.random()*areaH);
}

if ((e.target.x - e.target.width / 2) > areaW)
{
e.target.x = 0 - (e.target.height / 2);
e.target.y = Math.floor(Math.random()*areaW);
}
}

/* Move Left function */

private function moveLeft(e:Event):void
{
e.target.x-=e.target.ySpeed;
e.target.y-=e.target.xSpeed;
/* Reset light position, Y first, then X */
if (e.target.y - (e.target.height / 2) > areaH || e.target.y + (e.target.height / 2) < 0)
{
e.target.x = areaW + (e.target.width / 2);
e.target.y=Math.floor(Math.random()*areaH);
}
if ((e.target.x + e.target.width / 2) < 0)
{
e.target.x = areaW + (e.target.width / 2);
e.target.y=Math.floor(Math.random()*areaW);
}
}
}
}
.