05 April 2011

Flash AS3 Laser Lab Code

As promised, here is the code for Laser Lab. In order to run this code successfully, you'll need to have Slider and Color Picker controls in your file library. Everything else is pretty simple, just paste the code into first frame of your FLA file and that's it.

Original document dimensions are 550x400. That is why loop function exist to avoid drawing over control elements.

If you extend this example even more, do let me know.

// laser effect playground
// author http://flanture.blogspot.com
// based on code by www.eyes-squared.co.uk
// march 2011

import flash.display.Sprite;
import flash.filters.GlowFilter;
import flash.events.MouseEvent;
import flash.events.Event;
import fl.controls.ColorPicker;
import fl.controls.Slider;
import fl.events.ColorPickerEvent;
import fl.events.SliderEvent;

// starting values
var baseColor = 0xFFFFFF;
var glowColor = 0xFF0000;
var baseStrength = 3;
var glowStrength = 4;
var glowBlur = 8;

var sprite:Sprite = new Sprite();
addChild(sprite);

function updateFilter() {
var gf:GlowFilter = new GlowFilter(glowColor);
gf.quality = 3;
gf.strength = glowStrength;
gf.blurX = gf.blurY = glowBlur;
var fils = new Array();
fils.push(gf);

sprite.filters = fils;
}
updateFilter();

stage.addEventListener(MouseEvent.MOUSE_DOWN, startFire);
stage.addEventListener(MouseEvent.MOUSE_UP, stopFire);

function startFire(m:MouseEvent) {
sprite.graphics.lineStyle(baseStrength, baseColor);
sprite.graphics.moveTo(mouseX, mouseY);
addEventListener(Event.ENTER_FRAME, loop);
}

function stopFire(m:MouseEvent) {
removeEventListener(Event.ENTER_FRAME, loop);
}

function loop(e:Event) {
// avoid drawing on control elements
if (mouseX < 400) {
sprite.graphics.lineTo(mouseX, mouseY);
}
}

var cp1:ColorPicker = new ColorPicker();
cp1.width = 100;
cp1.height = 25;
cp1.x = 420;
cp1.y = 30;
addChild(cp1);

var cp2:ColorPicker = new ColorPicker();
cp2.width = 100;
cp2.height = 25;
cp2.x = 420;
cp2.y = 200;
addChild(cp2);

var sl1:Slider = new Slider();
sl1.x = 430;
sl1.y = 100;
sl1.liveDragging = true;
sl1.minimum = 1;
sl1.maximum = 10;
sl1.tickInterval = 1;
addChild(sl1);

var sl2:Slider = new Slider();
sl2.x = 430;
sl2.y = 280;
sl2.liveDragging = true;
sl2.minimum = 1;
sl2.maximum = 10;
sl2.tickInterval = 1;
addChild(sl2);

var sl3:Slider = new Slider();
sl3.x = 430;
sl3.y = 350;
sl3.liveDragging = true;
sl3.minimum = 1;
sl3.maximum = 7;
sl3.tickInterval = 1;
addChild(sl3);

cp1.addEventListener(ColorPickerEvent.CHANGE, changeHandler1);
function changeHandler1(event:ColorPickerEvent):void {
sprite.graphics.clear();
baseColor = event.target.selectedColor;
}
cp2.addEventListener(ColorPickerEvent.CHANGE, changeHandler2);
function changeHandler2(event:ColorPickerEvent):void {
glowColor = event.target.selectedColor;
sprite.graphics.clear();
updateFilter();
}
sl1.addEventListener(SliderEvent.CHANGE, changeSlider1);
function changeSlider1(event:SliderEvent):void
{
baseStrength = event.target.value;
sprite.graphics.clear();
}
sl2.addEventListener(SliderEvent.CHANGE, changeSlider2);
function changeSlider2(event:SliderEvent):void
{
glowStrength = event.target.value;
sprite.graphics.clear();
updateFilter();
}
sl3.addEventListener(SliderEvent.CHANGE, changeSlider3);
function changeSlider3(event:SliderEvent):void
{
var temp = 1;
for(var i=0; i<event.target.value; i++){
temp = 2 * temp;
}
glowStrength = temp;
sprite.graphics.clear();
updateFilter();
}


*_*

0 comments:

 

template by blogger templates