うお
package {
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.filters.GlowFilter;
import flash.geom.Point;
import flash.events.Event;
import flash.utils.*;
[SWF(backgroundColor="0")]
public class NoiseField extends Sprite {
//
private var field_array:Array = [];
private var w:int = 31;
private var h:int = 31;
private var rectX:int;
private var rectY:int;
//
private var perlin_bmd:BitmapData;
private var seed:int = Math.round(Math.random() * 100);
private var speed:int = 1;
private var offsetPoint:Point = new Point(0, 0);
private var damping:Number = .000007;
public function NoiseField() {
filters = [new GlowFilter(0x330033, .8, 5, 5, 3, 2), new GlowFilter(0xFFFFFF, .4, 12, 12, 1, 2)];
rectX = stage.stageWidth / w;
rectY = stage.stageWidth / h;
for (var r:Number = 0; r < h; r++) {
field_array[r] = [];
for (var c:Number = 0; c < w; c++) {
field_array[r][c] = {x:c, y:r, vx:0, vy:0, p:0};
}
}
perlin_bmd = new BitmapData(w, h);
perlin_bmd.perlinNoise(w / 3, h / 3, 1, seed, false, false, 1, true, [offsetPoint]);
upDateField();
setInterval(upDateField, 1000);
setInterval(addFish, 150);
}
private function upDateField():void {
offsetPoint.offset(speed, 0);
perlin_bmd.perlinNoise(w / 3, h / 3, 1, seed, false, false, 1, true, [offsetPoint]);
for (var r:Number = 0; r < h; r++) {
for (var c:Number = 0; c < w; c++) {
field_array[r][c]["p"] = perlin_bmd.getPixel(c, r) * damping;
}
}
for (r = 1; r < h - 1; r++) {
for (c = 1; c < w - 1; c++) {
field_array[r][c]["vx"] = (field_array[r - 1][c]["p"] - field_array[r + 1][c]["p"]);
field_array[r][c]["vy"] = (field_array[r][c + 1]["p"] - field_array[r][c - 1]["p"]);
}
}
}
private function addFish():void {
var fish:Fish = new Fish();
fish.x = root.mouseX;
fish.y = root.mouseY;
fish.addEventListener(Event.ENTER_FRAME, swim);
fish.addEventListener("remove", remove);
addChild(fish);
fish = null;
}
private function swim(event:Event):void {
event.target.count++;
event.target.xp = Math.round(event.target.x / rectX)
event.target.yp = Math.round(event.target.y / rectY);
try { event.target.vx += (field_array[event.target.yp][event.target.xp]["vx"] - event.target.vx) / 3 } catch (e:Error) { event.target.vx = event.target.vx; }
try { event.target.vy += (field_array[event.target.yp][event.target.xp]["vy"] - event.target.vy) / 3 } catch (e:Error) { event.target.vy = event.target.vy; }
event.target.x += event.target.vx < 0 ? Math.floor(event.target.vx) : Math.ceil(event.target.vx);
event.target.y += event.target.vy < 0 ? Math.floor(event.target.vy) : Math.ceil(event.target.vy);
event.target.rotation = twoPointToAngle(0, event.target.vx, 0, event.target.vy);
if ((event.target.x < -event.target.width) || (event.target.y < -event.target.height) || (event.target.x > stage.stageWidth + event.target.width) || (event.target.y > stage.stageHeight + height) || (event.target.count > 150)) {
event.target.removeEventListener(Event.ENTER_FRAME, swim);
event.target.dispatchEvent(new Event("remove"));
}
}
private function twoPointToAngle(x1:Number, x2:Number, y1:Number, y2:Number):Number {
var tmpX:Number = x1 - x2;
var tmpY:Number = y1 - y2;
var angle:Number = (Math.atan2(tmpY, tmpX) * 180 / Math.PI);
return angle;
}
private function remove(event:Event):void {
var clip:Fish = event.target as Fish;
removeChild(clip);
clip = null;
}
}
}
import flash.display.Shape;
import flash.display.Sprite;
import flash.utils.*;
class Fish extends Sprite {
public var vx:Number;
public var vy:Number;
public var xp:Number;
public var yp:Number;
public var count:uint = 0;
private var graph1:Shape;
private var graph2:Shape;
public function Fish() {
count = 0;
vx = Math.random() * 50 - 25;
vy = Math.random() * 50 - 25;
graph1 = new Shape();
graph1.graphics.beginFill(0xFFFFFF);
graph1.graphics.moveTo(0, 0);
graph1.graphics.curveTo(1.4, -2.1, 8, -2.5);
graph1.graphics.curveTo(10, -4.5, 11, -4.5);
graph1.graphics.curveTo(11, -2.5, 10, -2.5);
graph1.graphics.curveTo(14, -2.5, 22, -1.5);
graph1.graphics.curveTo(26.6, -3, 27, -2);
graph1.graphics.curveTo(26.6, -1, 26, -1);
graph1.graphics.curveTo(27.6, -1, 28.8, 1.2);
graph1.graphics.curveTo(27.6, 2.5, 22, -.5);
graph1.graphics.curveTo(14, 2.5, 10, 2.5);
graph1.graphics.curveTo(11, 2.5, 11, 4.5);
graph1.graphics.curveTo(10, 4.5, 8, 2.5);
graph1.graphics.curveTo(1.4, 2.1, 0, 0);
graph1.graphics.endFill();
graph2 = new Shape();
graph2.graphics.beginFill(0xFFFFFF);
graph2.graphics.moveTo(0, 0);
graph2.graphics.curveTo(1.4, -2.1, 8, -2.5);
graph2.graphics.curveTo(10, -3.5, 11, -3.5);
graph2.graphics.curveTo(11, -2.5, 10, -2.5);
graph2.graphics.curveTo(14, -2.5, 22, .5);
graph2.graphics.curveTo(27.6, -2, 28.8, -1);
graph2.graphics.curveTo(27.6, 1, 26, 1);
graph2.graphics.curveTo(26.6, 2, 27, 2.2);
graph2.graphics.curveTo(26.6, 3.5, 22, 1.5);
graph2.graphics.curveTo(14, 2.5, 10, 2.5);
graph2.graphics.curveTo(11, 2.5, 11, 3.5);
graph2.graphics.curveTo(10, 3.5, 8, 2.5);
graph2.graphics.curveTo(1.4, 2.1, 0, 0);
graph2.graphics.endFill();
graph2.visible = false;
addChild(graph1);
addChild(graph2);
setInterval(anime, 100);
}
private function anime():void {
graph1.visible = !graph1.visible;
graph2.visible = !graph2.visible;
}
}
// メモリいっぱい使ってるな。。