Alien synth
SiONとFrocessingの勉強で作ったものをにぎやかしで、JAMにだしてみる。
それらしい感じで音がなる。
ステージクリックで、キャラクター作成
キャラクタードラッグ&ドロップで移動開始
動いているキャラクターをクリックで、移動停止
停止しているキャラクタークリックでキャラクター削除
同じ種類のキャラクターは同時に音を鳴らします。
上のほうにいると高音・下のほうにいると低音。
左右の位置でパン
/**
* Copyright ffffine ( http://wonderfl.net/user/ffffine )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qopR
*/
// forked from Event's Instrument
/*
* SionとFrocessingの勉強で作ったものをにぎやかしで、JAMにだしてみる。
* ステージクリックで、音キャラクター作成
* 音キャラクタードラッグ&ドロップで移動開始
* 動いているキャラクターをクリックで、移動停止
* 停止しているキャラクタークリックでキャラクター削除
* 同じ種類の音キャラクターは同時に音を鳴らします。
* 上のほうにいると高音・下のほうにいると低音。
* 左右の位置でパン
*/
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.filters.GlowFilter;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.ColorTransform;
import flash.display.GradientType;
import flash.display.InterpolationMethod;
import frocessing.color.ColorHSV;
import frocessing.color.ColorRGB;
[SWF(backgroundColor = "#000000", frameRate = "30", width = "465", height = "465")]
public class Main extends Sprite
{
private var _defultNum:uint = 2;
private var _holder:Sprite;
private var _bmp:Bitmap;
private var _bmpData:BitmapData;
private var _colorTransform:ColorTransform;
private var _count:uint = 0;
private var _list:Array = [];
private var _line:Shape;
private var _sionSet:sionSet;
private var _alienSet:AlienSet;
private var _canvas:Sprite;
private var _timer:Timer;
private var _mx:Number;
private var _my:Number;
private var _color1:uint;
private var _color2:uint;
private var _hue1:Number;
private var _hue2:Number;
private var _matrix:Matrix;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_sionSet = new sionSet();
_alienSet = new AlienSet();
_alienSet.addEventListener(Event.COMPLETE, alienSetComplete);
_canvas = new Sprite();
_matrix = new Matrix();
_matrix.createGradientBox(465, 465, Math.PI / 2, 0, 0);
_canvas.graphics.clear();
_hue1 = 180;
_hue2 = 160;
var hsb1:ColorHSV = new ColorHSV(_hue1, 1, 0.9);
var rgb1:ColorRGB = hsb1.toRGB();
var hsb2:ColorHSV = new ColorHSV(_hue2, 1, 0.9);
var rgb2:ColorRGB = hsb2.toRGB();
_color1 = rgb1.r << 16 | rgb1.g << 8 | rgb1.b;
_color2 = rgb2.r << 16 | rgb2.g << 8 | rgb2.b;
_canvas.graphics.clear();
_canvas.graphics.beginGradientFill(GradientType.LINEAR,[ _color1,_color2],[1,1],[0,255],_matrix,"pad",InterpolationMethod.LINEAR_RGB);
_canvas.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
_canvas.graphics.endFill();
addChild(_canvas);
}
private function alienSetComplete(e:Event):void
{
_holder = new Sprite();
_bmpData = new BitmapData(stage.stageWidth, stage.stageHeight,true,0xcc0000);
_colorTransform = new ColorTransform(1, 1, 1, 0.9, 1.02, 1.02, 1.02,-40);
_bmp = new Bitmap(_bmpData);
_bmp.blendMode = "add";
_bmpData.colorTransform (_bmpData.rect, _colorTransform);
addChild(_bmp);
addChild(_holder);
_line = new Shape();
_holder.addChild(_line);
for (var i:int = 0; i < _defultNum; i++)
{
var alien:Alien = new Alien(_sionSet,_alienSet);
alien.x = Math.random() * stage.stageWidth;
alien.y = Math.random() * stage.stageHeight;
alien.ax = Math.random() * 10 -5;
alien.ay = Math.random() * 10 -5;
_holder.addChild(alien);
_list.push(alien);
alien.addEventListener(MouseEvent.MOUSE_DOWN, xMouseDown);
}
_timer = new Timer(250);
_timer.addEventListener(TimerEvent.TIMER, xTimerEvent);
_timer.start();
stage.addEventListener(Event.ENTER_FRAME, xEnterFrame);
_canvas.addEventListener(MouseEvent.CLICK, xClick);
}
private function xMouseDown(e:MouseEvent):void
{
var alien:Alien = e.target as Alien;
alien.addEventListener(MouseEvent.MOUSE_UP, xMouseUp);
alien.addEventListener(MouseEvent.MOUSE_OUT, xMouseUp);
alien.removeEventListener(MouseEvent.MOUSE_DOWN, xMouseDown);
_mx = stage.mouseX;
_my = stage.mouseY;
_holder.addChildAt(alien, alien.parent.numChildren);
alien.startDrag();
}
private function xMouseUp(e:MouseEvent):void
{
var alien:Alien = e.target as Alien;
alien.removeEventListener(MouseEvent.MOUSE_UP, xMouseUp);
alien.removeEventListener(MouseEvent.MOUSE_OUT, xMouseUp);
alien.addEventListener(MouseEvent.MOUSE_DOWN, xMouseDown);
alien.stopDrag();
if ( 2 < Math.abs(_mx - stage.mouseX) && 2 < Math.abs(_my - stage.mouseY)) {
alien.ax = -(_mx - alien.x)/20;
alien.ay = -(_my - alien.y)/20;
}
if ( alien.ax == 0 && alien.ay == 0 ){
var num:uint = 0;
alien.tweenStop();
_holder.removeChild(alien);
for (var i:int = 0; i < _list.length; i++)
{
if (_list[i] == alien) { num = i };
}
_list.splice(num, 1);
if (_timer.delay < 250) { _timer.delay += 10;}
}
if ( 2 > Math.abs(_mx - stage.mouseX) && 2 > Math.abs(_my - stage.mouseY)) {
alien.ax = 0;
alien.ay = 0;
}
_mx = 0;
_my = 0;
}
private function xTimerEvent(e:TimerEvent):void
{
_count++;
if (_count >= 8) { _count = 0; }
if (_list.length != 0) {
for (var i:int = 0; i < _list.length; i++)
{
if ( _count == Alien(_list[i]).charaNum ) {
Alien(_list[i]).tweenPlay();
}
}
if (_count == 0) {
Alien(_list[_count]).tweenPlay();
}
}
}
private function xClick(e:MouseEvent):void
{
var ball:Alien = new Alien(_sionSet,_alienSet);
ball.x = stage.mouseX;
ball.y = stage.mouseY;
_holder.addChild(ball);
_list.push(ball);
ball.addEventListener(MouseEvent.MOUSE_DOWN, xMouseDown);
if (_timer.delay > 190) {
_timer.delay -= 10;
}
}
private function xEnterFrame(e:Event):void
{
for (var i:int = 0; i < _list.length; i++)
{
var targetAlien:Alien = Alien(_list[i]);
var nextAlien:Alien = Alien(_list[i + 1]);
if (targetAlien.x > stage.stageWidth || targetAlien.x < 0) { targetAlien.ax *= -0.96 };
if (targetAlien.y > stage.stageHeight || targetAlien.y < 0) { targetAlien.ay *= -0.96 };
targetAlien.x += targetAlien.ax;
targetAlien.y += targetAlien.ay;
if (targetAlien.ax != 0) { targetAlien.ax *= 0.999 }else if(targetAlien.ax < 0.5){targetAlien.ax = 0};
if (targetAlien.ay != 0) { targetAlien.ay *= 0.999 }else if(targetAlien.ay < 0.5){targetAlien.ay = 0};
}
_bmpData.draw(_holder);
_bmpData.colorTransform (_bmpData.rect, _colorTransform);
_hue1 += 0.2;
_hue2 += 0.2;
var hsb1:ColorHSV = new ColorHSV(_hue1, 1, 0.9);
var rgb1:ColorRGB = hsb1.toRGB();
var hsb2:ColorHSV = new ColorHSV(_hue2, 1, 0.9);
var rgb2:ColorRGB = hsb2.toRGB();
_color1 = rgb1.r << 16 | rgb1.g << 8 | rgb1.b;
_color2 = rgb2.r << 16 | rgb2.g << 8 | rgb2.b;
_canvas.graphics.clear();
_canvas.graphics.beginGradientFill(GradientType.LINEAR,[ _color1,_color2],[1,1],[0,255],_matrix,"pad",InterpolationMethod.LINEAR_RGB);
_canvas.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
_canvas.graphics.endFill();
//drawline();
}
//private function drawline():void
//{
//_line.graphics.clear();
//_line.graphics.lineStyle(1, 0xCCCCCC);
//for (var i:int = 0; i < _list.length; i++)
//{
//if (i == 0) {
//_line.graphics.moveTo(_list[i].x, _list[i].y);
//}else {
//_line.graphics.lineTo(_list[i].x, _list[i].y);
//}
//}
//_line.graphics.endFill();
//}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import frocessing.display.F5MovieClip2D;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.events.TweenEvent;
import org.libspark.betweenas3.easing.*
import org.si.sion.sequencer.SiMMLTrack;
import org.si.sion.SiONDriver;
import org.si.sion.SiONVoice;
import org.si.sion.events.SiONEvent;
import org.si.sion.events.SiONTrackEvent;
import org.si.sion.utils.SiONPresetVoice;
import org.si.sion.SiONData;
import flash.display.BlendMode;
import flash.display.PixelSnapping;
class Alien extends F5MovieClip2D {
private var ss:sionSet;
private var aliens:AlienSet;
public var ax:Number =0;
public var ay:Number = 0;
public var charaNum:uint = 1;
public var xHue:uint = 0;
public var tween:ITween;
public function Alien(sionset:sionSet,alienSet:AlienSet):void {
ss = sionset;
aliens = alienSet;
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
create();
}
private function create():void
{
xHue = Math.floor(Math.random() * 360);
if (stage.mouseX == stage.stageWidth) { charaNum = Math.random() * 8} else {charaNum = Math.floor(stage.mouseX / stage.stageWidth * 8);};
var bmpData:BitmapData = aliens.bmpList[charaNum];
//colorMode(HSV);
//blendMode = BlendMode.ADD;
noStroke();
//tint(xHue,255,180);
imageMode(CENTER);
image(bmpData, 0, 0);
mouseChildren = false;
buttonMode = true;
var t:ITween = BetweenAS3.tween(this, { scaleX:1.5, scaleY:1.5 }, null, 0.25, Sine.easeInOut);
var t2:ITween = BetweenAS3.reverse(t);
var t3:ITween = BetweenAS3.func(soundPlay);
tween = BetweenAS3.serial(t3,t,t2);
}
public function tweenPlay():void {
var exSh:F5MovieClip2D = new F5MovieClip2D();
exSh.noStroke();
exSh.colorMode(HSV);
exSh.fill(xHue, 200, 200);
exSh.circle(0, 0,Math.random()*10+20);
exSh.alpha = 0.6;
addChildAt(exSh,0);
exSh.mouseEnabled = false;
var t:ITween = BetweenAS3.tween(exSh, { scaleX:3, scaleY:3, alpha:0 },null, 0.5,Sine.easeInOut);
t.addEventListener(TweenEvent.COMPLETE, function():void { removeChild(exSh);} );
t.play();
tween.play();
}
public function tweenStop():void {
tween.stop();
tween = null;
}
private function soundPlay():void
{
var percent:Number = Math.floor(this.y / stage.stageHeight*13);
var track:SiMMLTrack = ss.sionDriver.noteOn(ss.note[percent], ss.voice, 1, 0, 0 );
track.setEventTrigger(0, 1, 1);
track.pan = x/stage.stageWidth*128-64;
}
}
class sionSet {
public var presetVoiceList:SiONPresetVoice;
public var voice:SiONVoice;
public var sionDriver:SiONDriver;
public var data:SiONData;
public var note:Array;
public function sionSet() {
presetVoiceList = new SiONPresetVoice();
voice = new SiONVoice(5,3,63,63,0);
//voice = presetVoiceList["valsound.strpad24"];
sionDriver = new SiONDriver(2048);
note = [84, 82, 79, 77, 72, 70, 67, 65, 60, 55, 48, 43, null];
var mml:String = "@v128;";
mml += "#EFFECT0{reverb 100,70,80}";
var data:SiONData = sionDriver.compile(mml);
sionDriver.play(data);
}
}
class AlienSet extends EventDispatcher {
static public const ALIEN01:String = "http://assets.wonderfl.net/images/related_images/a/a6/a66b/a66bfd681e9ae6df332ddc86175c856edf4676e1";
static public const ALIEN02:String = "http://assets.wonderfl.net/images/related_images/4/4f/4f76/4f764a0364550213edaedef7b9de29a3059a0bae";
static public const ALIEN03:String = "http://assets.wonderfl.net/images/related_images/1/10/10a2/10a2e62660241966463250fee5337799ea6ccd75";
static public const ALIEN04:String = "http://assets.wonderfl.net/images/related_images/4/42/425d/425de38ac7617ad8ac50562f695372474f0ddbc8";
static public const ALIEN05:String = "http://assets.wonderfl.net/images/related_images/1/16/167e/167ea57f0e6e0ba030f802629f56f5a12ef8b1a2";
static public const ALIEN06:String = "http://assets.wonderfl.net/images/related_images/a/a2/a2af/a2afcb83a659f2cd60b47354729b6fce1bc8c2d0";
static public const ALIEN07:String = "http://assets.wonderfl.net/images/related_images/e/e1/e15f/e15f4587c48fd22f5230764684db010e8a6f9a26";
static public const ALIEN08:String = "http://assets.wonderfl.net/images/related_images/9/98/9832/9832cf17a7aa68c97f8eb6bb74fcca6781230cb9";
public var bmpList:Array = [];
private var loadCompleteCount:uint = 0;
public function AlienSet() {
loadBMP(ALIEN01);
loadBMP(ALIEN02);
loadBMP(ALIEN03);
loadBMP(ALIEN04);
loadBMP(ALIEN05);
loadBMP(ALIEN06);
loadBMP(ALIEN07);
loadBMP(ALIEN08);
}
private function loadBMP(in_URL:String):void
{
var loader:Loader = new Loader();
loader.load(new URLRequest(in_URL),new LoaderContext(true));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, xLoadComplete);
}
private function xLoadComplete(e:Event):void
{
e.target.removeEventListener(Event.COMPLETE, xLoadComplete);
var bmp:Bitmap = LoaderInfo(e.target).content as Bitmap;
var bmpData:BitmapData = bmp.bitmapData;
bmpList.push(bmpData);
loadCompleteCount++;
if (loadCompleteCount == 8) {
dispatchEvent(new Event(Event.COMPLETE));
}
}
}