for Lecture
package {
import flash.display.*;
import flash.events.Event;
import flash.filters.*;
import flash.geom.*;
public class FlashTest extends Sprite {
/*
*Step.0初期化
*/
public function start():void{
createManyBalls();
addEventListener(Event.ENTER_FRAME,moveBall);
addEventListener(Event.ENTER_FRAME, updateShape);
addEventListener(Event.ENTER_FRAME, updateFilter);
addEventListener(Event.ENTER_FRAME, duplicateImage);
}
/*
*Step.1ボールを作る
*createCircle(100);
*/
public function createCircle(r:Number):Shape{
var s:Shape = new Shape();
var g:Graphics = s.graphics;
g.beginFill(0x0ff0000);
g.drawCircle(0, 0, r);
// addChild(s);
return s;
}
/*
*Step.2たくさんボールを作る
*/
public function createManyBalls():void{
var c:Shape;
var p:Object;
var l:int = 30;
for (var i:int = 0; i < l; i++) {
c = createCircle(30+ 50 * Math.random());
//c.filters = [blur];
//c.x = Math.random()*500;
//c.y = Math.random()*500;
p = new Object();
p.speed = Math.random()*0.1;
p.r = 10 + 10* Math.random();
p.theta = Math.random() * PI * 2;
p.vtheta = .1*(Math.random()*2-1);
balllist[i] = c;
poslist[i] = p;
metaball.addChild(c);
}
//addChild(metaball);
}
/*
*Step.3ボールを動かす
* addEventListener(Event.ENTER_FRAME,moveBall);
*/
public function moveBall(e:Event):void{
var c:Shape;
var p:Object;
var l:int = balllist.length;
for (var i:int = 0; i < l; i++) {
c = balllist[i];
p = poslist[i];
p.theta+=p.vtheta;
c.x = p.r*Math.cos(p.theta)+center.x;
c.y = p.r*Math.sin(p.theta)+center.y;
//c.x +=(mouseX-c.x)*p.speed;
//c.y+=(mouseY-c.y)*p.speed;
}
}
/*
*Step.4ボールをぼかす
* c.filters = [blur];
*/
/*
*Step.5エッジを調整する
* addEventListener(Event.ENTER_FRAME, updateShape);
*/
private function updateShape(e:Event):void {
bmpd.fillRect(bmpd.rect,0x00000000);
bmpd.draw(metaball);
bmpd.threshold(bmpd,bmpd.rect,new Point(0, 0),">",0x30000000,0xff000000);
main_bmpd.fillRect(main_bmpd.rect,0x00000000);
main_bmpd.draw(holder);
}
/*
*Step.6 GlowFilterで光らせる
* addEventListener(Event.ENTER_FRAME, updateFilter);
* 背景黒にする
*/
private function updateFilter(e:Event):void {
//*
if(++color_count% interval==0){
huemarker += Math.random()*180;
interval = Math.random()*10+10;
color_count= 0;
}
//*/
hue += (huemarker-hue)*0.1;
//hue = Math.random()*360;
glow_out.color=glow_in.color=getRGB(hue,saturation,blightness);
bmp.filters=[glow_in,glow_out];
}
/*
*Step.7 輪を複製する
* addEventListener(Event.ENTER_FRAME, duplicateImage);
* initialize()内を修正
* //addChild(holder);
* addChild(main);
* main_bmpd.fillRect(main_bmpd.rect,0x00000000);
* main_bmpd.draw(holder);
*/
private function duplicateImage(e:Event = null):void {
//*
if (++bmp_count%5==0) bmp_count=0;
else return;
//*/
var bd:BitmapData = new BitmapData(stage.stageWidth/2,stage.stageHeight/2,true,0xff000000);
bd.copyPixels(main_bmpd,main_bmpd.rect,new Point());
var b:Bitmap = new Bitmap(bd);
b.blendMode = BlendMode.ADD;
b.alpha = .5;
b.x=main.width/2;
b.y=main.height/2;
addChildAt(b,1);
b.addEventListener(Event.ENTER_FRAME, fadeImage);
}
/*
*Step.8 複製した輪を消す
*/
private function fadeImage(e:Event):void {
var b:Bitmap = e.target as Bitmap;
b.alpha-=.005;
if(b.alpha<0){
b.removeEventListener(Event.ENTER_FRAME, fadeImage);
b.bitmapData.dispose();
removeChild(b);
}
}
private const PI:Number = Math.PI;
private var center:Object = {};
private var bmpd:BitmapData;
private var bmp:Bitmap;
private var main_bmpd:BitmapData;
private var main:Bitmap;
private var bmp_count:int = 0;
private var color_count:int = 0;
private var interval:int = Math.random()*30+30;
private var glow_in:GlowFilter;
private var glow_out:GlowFilter;
private var blur:BlurFilter;
private var huemarker:Number = Math.random()*360;
private var hue:Number = Math.random()*360;
private var saturation:Number = 100;
private var blightness:Number = 100;
private var holder:Sprite;
private var metaball:Sprite;
private var balllist:Array = [];
private var poslist:Array = [];
public function FlashTest() {
initialize();
start();
}
public function initialize():void{
stage.quality = StageQuality.LOW;
stage.frameRate = 64;
center.x = stage.stageWidth/4>>0;
center.y = stage.stageHeight/4>>0;
var s:Shape = new Shape();
var g:Graphics = s.graphics;
g.beginFill(0x000000);
g.drawRect(0,0,100,100);
s.width = stage.stageWidth;
s.height = stage.stageHeight;
addChild(s);
blur = new BlurFilter(16, 16, 2);
glow_in = new GlowFilter(0,.8,8,8,2,2,true);
glow_out = new GlowFilter(0,.8,16,16,2.5,2);
holder = new Sprite();
metaball = new Sprite();
bmpd = new BitmapData(stage.stageWidth/2,stage.stageHeight/2,true,0xffffffff);
bmp = new Bitmap(bmpd);
holder.x = stage.stageWidth/4;
holder.y = stage.stageHeight/4;
holder.addChild(bmp);
//addChild(holder);
main_bmpd = new BitmapData(stage.stageWidth/2,stage.stageHeight/2,true,0xff000000);
main = new Bitmap(main_bmpd);
main.blendMode = BlendMode.ADD;
main.x=main.width/2;
main.y=main.height/2;
addChild(main);
}
public const RGB_MAX:int = 255; public const H_MAX:int = 360; public const SB_MAX:int = 100;
public function getRGB(h:Number = 0, s:Number = 100 , b:Number=100):uint {
h = h % H_MAX >> 0;
s = Math.max(0, Math.min(s , SB_MAX));
b = Math.max(0, Math.min(b , SB_MAX));
var red:Number,green:Number,blue:Number;
if (s == 0){
red = green = blue = b*RGB_MAX/SB_MAX;
}else{
var hi:Number = Math.floor(h / (H_MAX / 6)) % 6;
var f:Number = h / (H_MAX / 6) - hi;
var p:Number = b / SB_MAX * ( 1 - s / SB_MAX );
var q:Number = b / SB_MAX * ( 1 - s / SB_MAX * f);
var t:Number = b / SB_MAX * ( 1 - s / SB_MAX * ( 1 - f));
switch (hi) {
case 0 : red = b / SB_MAX; green = t; blue = p; break;
case 1 : red = q; green = b / SB_MAX; blue = p; break;
case 2 : red = p; green = b / SB_MAX; blue = t; break;
case 3 : red = p; green = q; blue = b / SB_MAX; break;
case 4 : red = t; green = p; blue = b / SB_MAX; break;
case 5 : red = b / SB_MAX; green = p; blue = q; break;
}
red = Math.round(red * RGB_MAX);
green = Math.round(green * RGB_MAX);
blue = Math.round(blue * RGB_MAX);
}
return red << 16 | green << 8 | blue;
}
}
}