LightWater + SoundMixer
////////////////////////////////////////////////////////////////////////////////
// LightWater + SoundMixer
//
// [AS3.0] 燃える火・きらめく星・流れる水
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1047
//
// 音はTsabeat より拝借
// http://www.ektoplazm.com/free-music/tsabeat-warp-speed-ep/
////////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/pcEA
*/
////////////////////////////////////////////////////////////////////////////////
// LightWater + SoundMixer
//
// [AS3.0] 燃える火・きらめく星・流れる水
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1047
//
// 音はTsabeat より拝借
// http://www.ektoplazm.com/free-music/tsabeat-warp-speed-ep/
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
//import net.hires.debug.Stats;
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var water:LightWater;
private var playing:Boolean = true;
public function Main() {
//Wonderfl.capture_delay(4);
init();
//addChild(new Stats());
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
var rect:Rectangle = new Rectangle(0, 0, 465, 465);
water = new LightWater(rect);
addChild(water);
var area:Rectangle = new Rectangle(32, 80, 400, 64);
water.setup(area);
water.color = [0x3399FF, 0x66CCFF, 0x99FFFF];
//water.start();
}
}
}
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
import flash.display.BlendMode;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.net.URLRequest;
import flash.system.Security;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.easing.Quad;
import org.libspark.betweenas3.events.TweenEvent;
class LightWater extends Sprite {
private var rect:Rectangle;
private var bitmapData:BitmapData;
private var bitmap:Bitmap;
private static var ids:Array = [0, 0, 0, 0, 0, 1, 1, 2, 3];
private var colors:Array;
private var particles:Array;
private var area:Rectangle;
private static var targetPos:int = 500;
private static var policyPath:String = "http://mutast.heteml.jp/crossdomain.xml";
private var sound:Sound;
private var channel:SoundChannel;
private static var soundPath:String = "http://mutast.heteml.jp/works/music/music.mp3";
private var byteArray:ByteArray;
private static var channels:uint = 256;
private var factors:uint = 0;
public function LightWater(r:Rectangle) {
rect = r;
mouseChildren = false;
init();
}
private function init():void {
bitmapData = new BitmapData(rect.width, rect.height, false, 0xFF000000);
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
particles = new Array();
//
Security.loadPolicyFile(policyPath);
sound = new Sound();
sound.addEventListener(Event.COMPLETE, loaded, false, 0, true);
sound.load(new URLRequest(soundPath), new SoundLoaderContext(10, true));
byteArray = new ByteArray();
}
public function setup(a:Rectangle):void {
area = a;
}
public function set color(list:Array):void {
colors = list;
colors.push(0xFFFFFF);
}
private function loaded(evt:Event):void {
evt.target.removeEventListener(Event.COMPLETE, loaded);
start();
}
private function start():void {
channel = sound.play(0, 1000);
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
private function update(evt:Event):void {
SoundMixer.computeSpectrum(byteArray, true, factors);
var units:Array = new Array();
for (var c:uint = 0; c < 2; c++) {
for (var n:uint = 0; n < channels; n++) {
var p:Number = byteArray.readFloat();
if (p > 0.5) units.push(p);
}
}
create(units);
draw();
}
private function create(units:Array):void {
for (var n:uint = 0; n < units.length/2; n++) {
var id:uint = ids[Math.floor(Math.random()*(ids.length+1))];
var water:Particle = new Particle(colors[id]);
water.x = area.x + Math.random()*area.width;
water.y = area.y + Math.random()*area.height;
particles.push(water);
var itween:ITween = BetweenAS3.serial(
BetweenAS3.tween(water, {alpha: 1, scaleY: 0.75}, {alpha: 0, scaleY: 0.5}, 0.2, Quad.easeOut),
BetweenAS3.to(water, {y: targetPos, alpha: 0, scaleY: 1}, 0.8, Quad.easeIn)
);
itween.addEventListener(TweenEvent.COMPLETE, complete, false, 0, false);
itween.play();
}
}
private function complete(evt:TweenEvent):void {
evt.target.removeEventListener(TweenEvent.COMPLETE, complete);
var water:Particle = evt.target.getTweenAt(0).target;
particles.shift();
}
private function draw():void {
bitmapData.lock();
bitmapData.fillRect(rect, 0xFF000000);
for (var n:uint = 0; n < particles.length; n++) {
var water:Particle = particles[n];
var matrix:Matrix = new Matrix();
matrix.translate(water.x, water.y);
var colorTrans:ColorTransform = new ColorTransform();
colorTrans.color = water.color;
colorTrans.alphaMultiplier = water.alpha;
bitmapData.draw(water, matrix, colorTrans, BlendMode.ADD);
}
bitmapData.unlock();
}
}
import flash.display.Shape;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;
class Particle extends Shape {
private static var _width:uint = 40;
private static var _height:uint = 80;
private static var yOffset:int = 20;
private static var bColor:uint = 0xFFFFFF;
public var color:uint;
public function Particle(c:uint) {
color = c;
draw();
}
private function draw():void {
var colors:Array = [bColor, bColor];
var alphas:Array = [1, 0];
var ratios:Array = [0, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(_width, _height, 0, -_width*0.5, -_height*0.5+yOffset);
graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
graphics.drawEllipse(-_width*0.5, -_height*0.5+yOffset, _width, _height);
graphics.endFill();
}
}