flash on 2011-1-30
「.fla2」を参考にしてます.
声で画面が震えます.
色々適当すぎる
/**
* Copyright yamatoo ( http://wonderfl.net/user/yamatoo )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ak9a
*/
// 「.fla2」を参考にしてます.
// 声で画面が震えます.
// 色々適当すぎる
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.Microphone;
import flash.media.SoundTransform;
import flash.media.Video;
public class MicDeGunyaGunya extends Sprite {
private static const _W:Number = 465;
private static const _H:Number = 465;
private var _cam:Camera;
private var _vid:Video;
private var _mic:Microphone;
private var _bmp:Bitmap;
private var _src:BitmapData;
private var _j:Jelly;
private var _powerRect:Rectangle;
public function MicDeGunyaGunya()
{
if(stage) _init();
else addEventListener(Event.ADDED_TO_STAGE, _init, false, 0, true);
}
private function _init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, _init);
stage.align = "TL";
stage.scaleMode = "noScale";
stage.quality = "high";
_getCamera();//カメラの取得
}
private function _getCamera():void
{
_cam = Camera.getCamera();
if(_cam)
{
_cam.setMode(_W, _H, 30);
_vid = new Video(_W, _H);
_vid.attachCamera(_cam);
_getMic();
}
}
private function _getMic():void
{
_mic = Microphone.getMicrophone();
if(_mic)
{
_mic.setLoopBack(true);//マイクの音を取得
_mic.setUseEchoSuppression(true);//エコーの抑制
_mic.setSilenceLevel(1, 3000);//サイレンスの閾値
_mic.gain = 30;//ゲイン
var trans:SoundTransform = new SoundTransform();
trans.volume = 0;
_mic.soundTransform = trans;
_setup();
}
}
private function _setup():void
{
_src = new BitmapData(_W, _H, true, 0xffffff);
_j = addChild(new Jelly(_src, 12, 12)) as Jelly;
addEventListener(Event.ENTER_FRAME, _enterFrameHandler, false, 0, true);
}
private function _enterFrameHandler(e:Event):void
{
_src.draw(_vid);
var p:Number = _mic.activityLevel;
if(p > 0)
{
trace(p);
_powerRect = new Rectangle();
_powerRect.x = _W / 2 + (Math.random() - .5) * _W / (p * p);
_powerRect.y = _H / 2 + (Math.random() - .5) * _H / (p * p);
_powerRect.width = p / 10 * 300;
_powerRect.height = p / 10 * 300;
}else{
_powerRect = null;
}
_j.setPower(_powerRect);
}
}
}
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
class Jelly extends Sprite
{
private var _bitmapdata:BitmapData;
private var _wSegment:uint;
private var _hSegment:uint;
private var _vertices:Vector.<Number>;//頂点配列
private var _indices:Vector.<int>;//インデックス配列
private var _uvtData:Vector.<Number>;//UVT配列
private var _baseVertices:Vector.<Number>;//元の位置保存用配列
private var _poweredVertices:Vector.<Number>;//力の計算をした位置を保持配列
private var _energies:Vector.<Number>;//現在の速度の保持
private var _power:Rectangle;
public function Jelly(bitmapdata:BitmapData, wSegment:Number, hSegment:Number)
{
_bitmapdata = bitmapdata;
_wSegment = wSegment;
_hSegment = hSegment;
var numVertices:uint = (_wSegment + 1) * (_hSegment + 1) * 2;
var i:uint, x:uint, y:uint;
_vertices = new Vector.<Number>(numVertices, true);
_indices = new Vector.<int>(_wSegment * (6 * _hSegment), true);
_uvtData = new Vector.<Number>(numVertices, true);
_energies = _vertices.slice();
_poweredVertices = _vertices.slice();
var w:Number = _bitmapdata.width / _wSegment;
var h:Number = _bitmapdata.height / _hSegment;
for(x = 0; x <= _wSegment; x++)
{
for(y = 0; y <= _hSegment; y++)
{
i = (y * (_wSegment + 1) + x) * 2;
_vertices[i] = w * x;
_vertices[i + 1] = h * y;
_uvtData[i] = _vertices[i] / _bitmapdata.width;
_uvtData[i + 1] = _vertices[i + 1] / _bitmapdata.height;
}
}
_baseVertices = _vertices.slice();
for(x = 0; x < _wSegment; x++)
{
i = (x * (6 * _hSegment));
for(y = 0; y < _hSegment; y++)
{
_indices[i + y * 6 + 0] = (y + 0) * (_wSegment + 1) + (x + 0);
_indices[i + y * 6 + 1] = (y + 0) * (_wSegment + 1) + (x + 1);
_indices[i + y * 6 + 2] = (y + 1) * (_wSegment + 1) + (x + 0);
_indices[i + y * 6 + 3] = (y + 0) * (_wSegment + 1) + (x + 1);
_indices[i + y * 6 + 4] = (y + 1) * (_wSegment + 1) + (x + 0);
_indices[i + y * 6 + 5] = (y + 1) * (_wSegment + 1) + (x + 1);
}
}
addEventListener(Event.ENTER_FRAME, _enterFrameHandler, false, 0, true);
}
private function _enterFrameHandler(e:Event):void
{
var nLength:uint = (_wSegment + 1) * (_hSegment + 1) * 2;
for(var i:uint = 0; i < nLength; i++){
_poweredVertices[i] = _baseVertices[i];
}
if(_power != null){
_addPower(_power, _poweredVertices);
}
_calcEnergy();
var g:Graphics = this.graphics;
g.clear();
g.beginBitmapFill(_bitmapdata);
g.drawTriangles(_vertices, _indices, _uvtData);
g.endFill();
}
private function _calcEnergy():void
{
for(var x:uint = 0; x <= _wSegment; x++)
{
for(var y:uint = 0; y <= _hSegment; y++)
{
var i:uint = (y * (_wSegment + 1) + x) * 2;
_energies[i] = (_energies[i] + (_poweredVertices[i] - _vertices[i])) * 0.6;//vx += (tagetX - vx) * 0.
_energies[i + 1] = (_energies[i + 1] + (_poweredVertices[i + 1] - _vertices[i + 1])) * 0.6;//yv += (tagetY - vy) * 0.
_vertices[i] += _energies[i];//x
_vertices[i + 1] += _energies[i + 1];//y
}
}
}
private function _addPower(power:Rectangle, vertices:Vector.<Number>):void
{
var px:Number = power.x;
var py:Number = power.y;
var vx:Number = power.width;
var vy:Number = power.height;
var vl:Number = Math.sqrt(vx * vx + vy * vy);
var nvx:Number = vl != 0 ? vx / vl : 0;
var nvy:Number = vl != 0 ? vy / vl : 0;
for(var x:uint = 1; x < _wSegment; x++)
{
for(var y:uint = 1; y < _hSegment; y++)
{
var i:uint = (y * (_wSegment + 1) + x ) * 2;
var baseX:Number = _vertices[i];
var baseY:Number = _vertices[i + 1];
var dx:Number = baseX - px;
var dy:Number = baseY - py;
var d:Number = dx * dx + dy * dy;
var dl:Number = Math.sqrt(d);
var ndx:Number = dl != 0 ? dx / dl : 0;
var ndy:Number = dl != 0 ? dy / dl : 0;
var t:Number = Math.atan2(nvx * ndy - nvy * ndx, nvx * ndx + nvy * ndy);
var f:Number = 1.0 - Math.abs(t) / (Math.PI / 4);
if(f < 0)
{
f = 0;
}
var l:Number = 120.0 * (1.0 + f * 1.2);
var factor:Number = 1.0 -d / (l * l);
if(factor < 0)
{
factor = 0;
}
_vertices[i] = baseX + vx * factor;
_vertices[i + 1] = baseY + vy * factor;
}
}
}
public function setPower(power:Rectangle):void
{
_power = power;
}
}