TurkeyMarch(トルコ行進曲)
http://wonderfl.net/c/3abIの「VJ風にしてみた forked from: モアレっぽい感じ」を参考にBeatTimerとBetweenAS3の練習
timerHandlerの中身の部分をhttp://wonderfl.net/c/8muPの「四畳半神話大系的な何か」を参考に変更しました。
/**
* Copyright yabuchany ( http://wonderfl.net/user/yabuchany )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zJG9
*/
// forked from yabuchany's ColorCellophane
// forked from yabuchany's BeatTimerとBetweenAS3の練習
// http://wonderfl.net/c/3abIの「VJ風にしてみた forked from: モアレっぽい感じ」を参考にBeatTimerとBetweenAS3の練習
// timerHandlerの中身の部分をBetweenAS3の練習用に変更しました。
// timerHandlerの中身の部分をhttp://wonderfl.net/c/8muPの「四畳半神話大系的な何か」を参考に変更しました。
package
{
import flash.display.*;
import flash.display.Sprite;
import flash.events.*;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.easing.*;
import org.libspark.betweenas3.events.TweenEvent;
import org.libspark.betweenas3.tweens.ITween;
[SWF(backgroundColor="0", frameRate="60", width="465", height="465")]
public class TurkeyMarch extends Sprite
{
private var _phase:int;
private var _gain:int;
private var _sound:Sound;
private var _soundChannel:SoundChannel;
private var _beatTimer:BeatTimer;
private var _isOnBeat:Boolean;
private var _beatPosition:Number;
private const EZ:Number = .05; //速度が変わるよ!
private var _tw:ITween;
private var _container:Sprite = new Sprite();
private var MAX:int = 10;
private var _cnt:int = 0;
private var _topBox:Box;
private var _lastBox:Box;
private var bmdata:BitmapData = new BitmapData(465, 465, false, 0x000000);
private var bm:Bitmap = new Bitmap(bmdata);
//
private var twArray:Array = [];
private var w:Number = 200;
private var h:Number = 200;
private var box:Box = new Box(w, h);
public function TurkeyMarch():void
{
addChild(bm);
//addEventListener(Event.ENTER_FRAME, timerHandler);
_beatTimer = new BeatTimer();
_sound = new Sound();
_sound.load(new URLRequest("http://yabuchany.com/blog/wp-content/uploads/2010/12/src/turkeymarch.mp3"));
_sound.addEventListener(Event.COMPLETE, loadCompleteHandler);
_topBox = box;
_lastBox = box;
_container.x = stage.stageWidth/2;
_container.y = stage.stageHeight/2;
addChild(_container);
_container.addChild(box);
box.alpha = 0;
//trace(box.alpha);
_container.z = 20000;
//_tw = BetweenAS3.parallel(
//BetweenAS3.tween(box,{width:box.width},{width:0},EZ,Quint.easeInOut),
//BetweenAS3.tween(box,{height:box.height},{height:0},EZ,Quint.easeInOut));
//_tw.addEventListener(TweenEvent.COMPLETE,compTween);
//_tw.play();
}
private function compTween(e:TweenEvent):void
{
_tw.removeEventListener(TweenEvent.COMPLETE,compTween);
_cnt++;
var twArray:Array = [];
var w:Number = Math.random()*200 + 100;
var h:Number = Math.random()*200 + 100;
var box:Box = _topBox;
//途中の部屋
while(box.linkBox){
if(_lastBox.width>_lastBox.height){
twArray.push(BetweenAS3.parallel(
BetweenAS3.tween(box,{x:box.x - _lastBox.width/2},null,EZ,Quint.easeInOut),
BetweenAS3.tween(box,{y:box.y - _lastBox.height+2},null,EZ,Quint.easeInOut)
));
}else{
twArray.push(BetweenAS3.parallel(
BetweenAS3.tween(box,{x:box.x - _lastBox.width+2},null,EZ,Quint.easeInOut),
BetweenAS3.tween(box,{y:box.y - _lastBox.height/2},null,EZ,Quint.easeInOut)
));
}
box = box.linkBox;
}
//最後の部屋と新しい部屋
var box_new:Box = new Box(w,h);
_container.addChild(box_new); // ここで初めて次のBoxができる。
// ここを削るとつながらない部屋同士が
if(box.width>box.height){
twArray.push(BetweenAS3.parallel(
BetweenAS3.tween(box,{x:box.x - box.width/2},null,EZ,Quint.easeInOut),
BetweenAS3.tween(box,{y:box.y - box.height+2},null,EZ,Quint.easeInOut),
BetweenAS3.tween(box_new,{x:0},{x:box.width/2},EZ,Quint.easeInOut),
BetweenAS3.tween(box_new,{y:0},{y:box.height},EZ,Quint.easeInOut)
));
}else{
twArray.push(BetweenAS3.parallel(
BetweenAS3.tween(box,{x:box.x - box.width+2},null,EZ,Quint.easeInOut),
BetweenAS3.tween(box,{y:box.y - box.height/2},null,EZ,Quint.easeInOut),
BetweenAS3.tween(box_new,{y:0},{y:box.height/2},EZ,Quint.easeInOut)
));
}
// ここを削ると部屋はつばがるがTweenしない
twArray.push(BetweenAS3.parallel(
BetweenAS3.tween(box_new,{width:w},{width:0},EZ,Quint.easeInOut),
BetweenAS3.tween(box_new,{height:h},{height:0},EZ,Quint.easeInOut))
);
//新しい配列をリストに追加
box.linkBox = box_new;
_lastBox = box_new;
//20個以上になったら古いの消すよ
if(_cnt > 20){
var bufBox:Box = _topBox.linkBox;
_container.removeChild(_topBox);
_topBox = bufBox;
//_cnt = 0;
}
_tw = BetweenAS3.parallelTweens( twArray );
_tw.addEventListener(TweenEvent.COMPLETE,compTween);
_tw.play();
}
private function timerHandler(e:Event):void
{
_cnt++;
_beatTimer.update();
if ((_beatTimer.beatPosition | 0) % 8 == 0) {
if (! _isOnBeat) {
_tw.removeEventListener(TweenEvent.COMPLETE,compTween);
_cnt++;
var twArray:Array = [];
var w:Number = Math.random()*200 + 100;
var h:Number = Math.random()*200 + 100;
var box:Box = _topBox;
while(box.linkBox){
if(_lastBox.width>_lastBox.height){
twArray.push(BetweenAS3.parallel(
BetweenAS3.tween(box,{x:box.x - _lastBox.width/2},null,EZ,Quint.easeInOut),
BetweenAS3.tween(box,{y:box.y - _lastBox.height+2},null,EZ,Quint.easeInOut)
));
}else{
twArray.push(BetweenAS3.parallel(
BetweenAS3.tween(box,{x:box.x - _lastBox.width+2},null,EZ,Quint.easeInOut),
BetweenAS3.tween(box,{y:box.y - _lastBox.height/2},null,EZ,Quint.easeInOut)
));
}
box = box.linkBox;
box.alpha = 0;
}
//最後の部屋と新しい部屋
var box_new:Box = new Box(w,h);
_container.addChild(box_new); // ここで初めて次のBoxができる。
//新しい配列をリストに追加
box.linkBox = box_new;
_lastBox = box_new;
//4個以上になったら古いの消すよ
if(_cnt > 20){
var bufBox:Box = _topBox.linkBox;
_container.removeChild(_topBox);
_topBox = bufBox;
}
twArray.push(BetweenAS3.tween(_container,{rotationZ:Math.random() *360+45},null,.2,Quint.easeInOut),
BetweenAS3.tween(_container, { z:Math.random() * 2000 }, null, .8, Quint.easeInOut));
_tw = BetweenAS3.parallelTweens( twArray );
_tw.addEventListener(TweenEvent.COMPLETE,compTween);
_tw.play()
}
} else {
if (Math.random() > 0.5) {
_isOnBeat = false;
}
}
}
private function loadCompleteHandler(e:Event):void
{
_sound.removeEventListener(Event.COMPLETE, loadCompleteHandler);
addEventListener(Event.ENTER_FRAME, timerHandler);
soundStart();
_tw = BetweenAS3.parallel(
//BetweenAS3.tween(box,{width:box.width},{width:0},EZ,Quint.easeInOut),
//BetweenAS3.tween(box,{height:box.height},{height:0},EZ,Quint.easeInOut)
BetweenAS3.tween(_container, { z:Math.random() * 2000 }, null, .01, Quint.easeInOut)
);
_tw.addEventListener(TweenEvent.COMPLETE,compTween);
_tw.play();
}
private function soundStart():void
{
_soundChannel = _sound.play(0);
_beatTimer.start(120);
_soundChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleteHandler);
}
private function soundCompleteHandler(e:Event):void
{
soundStart();
}
}
}
import flash.display.Sprite;
import flash.utils.getTimer;
class Box extends Sprite
{
public var linkBox:Box = null;
public function Box(w:Number,h:Number):void
{
var lineColor:uint = 0xFFFFFF*Math.random();
var fillColor:uint = 0xFFFFFF*Math.random();
graphics.lineStyle(15,lineColor,.8,false,"none","square");
graphics.drawRect(0,0,w,h);
graphics.endFill();
}
}
class BeatTimer
{
public function BeatTimer()
{
}
private var _bpm:Number;
private var _startTime:uint;
private var _beatPosition:Number;
private var _phase:Number;
private var _isOnBeat:Boolean = false;
public function get bpm():Number
{
return _bpm;
}
public function get beatPosition():Number
{
return _beatPosition;
trace(_beatPosition);
}
public function get phase():Number
{
return _phase;
}
public function get isOnBeat():Boolean
{
return _isOnBeat;
}
public function start(bpm:Number):void
{
_bpm = bpm;
_startTime = getTimer();
update();
}
public function update():void
{
var currentTime:uint = getTimer();
var beatInterval:Number = (60 * 1000) / _bpm;
var oldPosition:Number = _beatPosition;
_beatPosition = (currentTime - _startTime) / beatInterval;
_phase = _beatPosition - int(_beatPosition);
_isOnBeat = int(oldPosition) != int(_beatPosition);
}
}