きれいなものを作ってみたかった(悲しみ)
完成品 (音出ます)
http://about-hiroppy.com/flash/work/sound1/
そーす(ファイル分割してないです。はい)
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.events.Event; import flash.events.TimerEvent; import flash.filters.GlowFilter; import flash.geom.Point; import flash.utils.Timer; import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamics.b2World; import caurina.transitions.Tweener; import frocessing.color.ColorHSV; [SWF(width="500",height="500",backgroundColor="0")] public class sound1 extends Sprite { private var world:b2World; private var gravity:b2Vec2 = new b2Vec2(0,9.8); private var Coordinate_x:Array = new Array(55,102.5,150,350,350,150,55,445,397.5,445); private var Coordinate_y:Array = new Array(450,350,250,250,450,450,250,250,350,450); private var sound_id:Array = new Array("1","2","3","4","5","6","7","8","9","10"); public static var effect_layer:Sprite = new Sprite(); private var boll_layer:Sprite = new Sprite(); private var bmpd_off:BitmapData = new BitmapData(30,30,false,0); private var first_flag:Boolean = false; public function sound1() { var N:int = Coordinate_x.length; world = new b2World(gravity,true); addChild(effect_layer); addChild(boll_layer); addChild(new Bitmap(bmpd_off)); //wall var b1:square = new square(world,1,0.6,0.9); b1.square_static(0,500,1,500); boll_layer.addChild(b1); var b2:square = new square(world,1,0.6,0.9); b2.square_static(500,500,1,500); boll_layer.addChild(b2); //color_select var color:ColorHSV; //sound_object for(var i:int=0;i<N;i++){ if(Coordinate_y[i] == 250) color = new ColorHSV(119,0.9,1,0.5); else if(Coordinate_y[i] == 350) color = new ColorHSV(200,0.9,1,0.5); else color = new ColorHSV(19,0.9,1,0.5); var c2:square = new square(world,1,0.6,0.6); c2.square_static_A(Coordinate_x[i],Coordinate_y[i],20,20,45,sound_id[i]); var o1:Sprite = new Sprite(); o1.graphics.lineStyle(3,0xffffff); o1.graphics.drawRect(0,0,40,40); o1.x = Coordinate_x[i]; o1.y = Coordinate_y[i]-30; o1.rotation = 45; o1.filters = [new GlowFilter(color.value,1,12,12,6,2)]; boll_layer.addChild(o1); } for(i=0;i<2;i++){ color = new ColorHSV(70,0.9,1,0.5); var c3:square = new square(world,1,0.6,0.6); c3.square_static_A(0+i*500,350,20,20,45,(String)(i+10)); var o2:Sprite = new Sprite(); o2.graphics.lineStyle(3,0xffffff); o2.graphics.drawRect(0,0,40,40); o2.x = 0+i*500; o2.y = 350-30; o2.rotation = 45; o2.filters = [new GlowFilter(color.value,1,12,12,6,2)]; boll_layer.addChild(o2); } // var debug:b2DebugDraw = new b2DebugDraw(); // debug.SetSprite(this); // debug.SetDrawScale(30.0); // debug.SetFillAlpha(0.3); // debug.SetLineThickness(1.0); // debug.SetFlags(b2DebugDraw.e_shapeBit); // world.SetDebugDraw(debug); // world.DrawDebugData(); world.SetContactListener(new contactListener); addEventListener(Event.ENTER_FRAME,onEnterFrame); var timer:Timer = new Timer(1300); timer.start(); timer.addEventListener(TimerEvent.TIMER,onTimerEvent); } private function onEnterFrame(e:Event):void{ var cnt:int=0; for(var body:b2Body = world.GetBodyList(); body != null; body = body.GetNext()){ if(body.GetUserData() != null && body.GetUserData() != undefined){ body.GetUserData().x = body.GetPosition().x*30; body.GetUserData().y = body.GetPosition().y*30; if(body.GetPosition().y >= 600/30){ world.DestroyBody(body); cnt++; } } } world.Step(1/30, 10 ,20); world.ClearForces(); } private function onTimerEvent(e:TimerEvent):void{ for(var i:int=0;i<2;i++){ var c:Circle = new Circle(world,1,0.6,0.8); c.Circle_5(240+i*20,530,9,Math.random()*0xffffff,true,"moveCircle",false); boll_layer.addChild(c); } var cnt:int=0; for(var body:b2Body = world.GetBodyList(); body != null; body = body.GetNext()){ if((body.GetPosition().x == 240/30 || body.GetPosition().x == 260/30) && body.GetPosition().y == 530/30){ if(!first_flag){ if(cnt == 0)body.SetLinearVelocity(new b2Vec2(-5,-30)); if(cnt == 1)body.SetLinearVelocity(new b2Vec2(6,-30)); first_flag = true; } else{ if(cnt == 0)body.SetLinearVelocity(new b2Vec2(-1*Math.floor(Math.random()*5+3),-30)); if(cnt == 1)body.SetLinearVelocity(new b2Vec2(Math.floor(Math.random()*5+3),-30)); } cnt++; // body.ApplyImpulse(new b2Vec2(10,300),new b2Vec2(10,300)); } } } public static function effect(xx:Number,yy:Number):void{ for(var i:int=0;i<5;i++){ var c:Sprite = new Sprite(); var points:Array = new Array(); var star_size:int=25; var color:uint = Math.floor(Math.random()*0xffffff); effect_layer.addChild(c); for (var degree:Number = 0; degree < 360; degree += 360 /5){ var tx:Number = xx + Math.cos(degree * Math.PI / 180) * star_size; var ty:Number = yy + Math.sin(degree * Math.PI / 180) * star_size; points.push(new Point(tx, ty)); if (true){ tx = xx + Math.cos((degree + 360 / 5 / 2) * Math.PI / 180) * (star_size / 2); ty = yy + Math.sin((degree + 360 / 5 / 2) * Math.PI / 180) * (star_size / 2); points.push(new Point(tx, ty)); } } c.graphics.lineStyle(8.0, 0xffffff); c.graphics.moveTo(points[0].x, points[0].y); for (var j:int = 1; j < points.length; j++){ c.graphics.lineTo(points[j].x, points[j].y); } c.graphics.lineTo(points[0].x, points[0].y); var angle:Number = Math.floor(Math.random() * 360); var length:Number = Math.floor(Math.random() * 120) + 50; var dx:Number = xx + length * Math.cos(angle); var dy:Number = yy + length * Math.sin(angle); c.filters = [new GlowFilter(color,1,12,12,6,2)]; Tweener.addTween(c,{ x:dx, y:dy, scaleX: 0, scaleY: 0, time:5, onComplete: function():void { effect_layer.removeChild(c); } }); } } public static function object_A_effect(xx:Number,yy:Number):void{ var A_effect:Sprite = new Sprite(); A_effect.graphics.lineStyle(4,0xffffff); A_effect.graphics.drawCircle(0,0,50); A_effect.x = xx*30; A_effect.y = yy*30; effect_layer.addChild(A_effect); Tweener.addTween(A_effect,{ x:xx*30, y:yy*30, scaleX: 0, scaleY: 0, time:3, onComplete: function():void { effect_layer.removeChild(A_effect); } }); } } } import flash.display.Sprite; import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundTransform; import flash.net.URLRequest; import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2ContactListener; import Box2D.Dynamics.Contacts.b2Contact; class contactListener extends b2ContactListener{ public var sound_list:Array = new Array("002.mp3","009.mp3","038.mp3","049.mp3","065.mp3","070.mp3","092.mp3","096.mp3"); public var piano_sound_list:Array = new Array( "PianoA.mp3","PianoB.mp3","PianoC.mp3","PianoC2.mp3", "PianoD.mp3","PianoE.mp3","PianoF.mp3","PianoG.mp3" ); public function contactListener():void{ } public override function BeginContact(p:b2Contact):void{ if(p.GetFixtureA().GetBody().GetUserData() != null && p.GetFixtureB().GetBody().GetUserData() != null){ // trace(p.GetFixtureA().GetBody().GetUserData()); // trace(p.GetFixtureB().GetBody().GetUserData()); var object_A:String = p.GetFixtureA().GetBody().GetUserData(); var object_B:String = p.GetFixtureB().GetBody().GetUserData(); if(object_A == "[object Object]"){ // trace(p.GetFixtureB().GetBody().GetUserData().id); sound1.object_A_effect(p.GetFixtureA().GetBody().GetPosition().x,p.GetFixtureA().GetBody().GetPosition().y); Sound_out(p.GetFixtureA().GetBody().GetUserData().id); } sound1.effect(p.GetFixtureA().GetBody().GetPosition().x*30,p.GetFixtureA().GetBody().GetPosition().y*30); } } private function Sound_out(s:String):void{ var text:String; if((int)(s) >= 11){ return; } else if(s == "9") text = "sound/"+sound_list[3]; else if(s == "10") text = "sound/"+sound_list[6]; else text = "piano_sound/"+(String)(piano_sound_list[(int)(s)-1]); var req:URLRequest = new URLRequest(text); var ss:Sound = new Sound(req); var ch:SoundChannel = ss.play(); var st:SoundTransform = new SoundTransform(1.0,0); st.volume = 0.5; ch.soundTransform = st; } } import flash.filters.GlowFilter; import flash.display.Sprite; import Box2D.Collision.Shapes.b2PolygonShape; import Box2D.Collision.Shapes.b2CircleShape; import Box2D.Dynamics.b2Body; import Box2D.Dynamics.b2BodyDef; import Box2D.Dynamics.b2FixtureDef; import Box2D.Dynamics.b2World; class Circle extends Sprite{ private var bodyDef:b2BodyDef = new b2BodyDef(); private var body:b2Body; private var fixtureDef:b2FixtureDef = new b2FixtureDef(); private var circleDef:b2CircleShape = new b2CircleShape(); private var world:b2World; public function Circle(W:b2World,den:Number,fri:Number,res:Number):void{ this.world = W; fixtureDef.shape = circleDef; fixtureDef.density = den; fixtureDef.friction = fri; fixtureDef.restitution = res; } public function Circle_2(position_x:Number,position_y:Number):b2Body{ var r:Number = 10; var color:uint = 0x00ffff; bodyDef.type = b2Body.b2_dynamicBody; add(r,position_x,position_y); return body; } public function Circle_5(position_x:Number,position_y:Number,r:Number,color:uint,type:Boolean,s:String,id:Boolean):b2Body{ if(type) bodyDef.type = b2Body.b2_dynamicBody; else bodyDef.type = b2Body.b2_staticBody; bodyDef.linearDamping = 0.95; // 速度減少 if(!id ) draw(r,color); else Id(s); add(r,position_x,position_y); return body; } private function draw(r:Number,color:uint):void{ bodyDef.userData = new Sprite(); bodyDef.userData.graphics.lineStyle(3,0xffffff); bodyDef.userData.graphics.drawCircle(0,0,r); filters = [new GlowFilter(color,1,18,18,6,2)]; // blendMode = BlendMode.ADD; //omoi addChild(bodyDef.userData); } private function Id(s:String):void{ bodyDef.userData = new Object(); bodyDef.userData.id = s; } private function add(r:Number,position_x:Number,position_y:Number):void{ circleDef.SetRadius(r/30); bodyDef.position.Set(position_x/30,position_y/30); body = world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); } } class square extends Sprite{ private var bodyDef:b2BodyDef = new b2BodyDef(); private var body:b2Body; private var fixtureDef:b2FixtureDef = new b2FixtureDef(); private var boxShape:b2PolygonShape = new b2PolygonShape(); private var world:b2World; public function square(W:b2World,den:Number,fri:Number,res:Number):void{ this.world = W; fixtureDef.shape = boxShape; fixtureDef.density = den; fixtureDef.friction = fri; fixtureDef.restitution = res; } public function square_static(position_x:Number,position_y,width:Number,height:Number):b2Body{ bodyDef.type = b2Body.b2_staticBody; add(position_x,position_y,width,height); return body; } public function square_dynamic(position_x:Number,position_y,width:Number,height:Number):b2Body{ bodyDef.type = b2Body.b2_dynamicBody; add(position_x,position_y,width,height); return body; } //角度付き public function square_static_A(position_x:Number,position_y,width:Number,height:Number,angle:Number,id:String):b2Body{ bodyDef.type = b2Body.b2_staticBody; bodyDef.angle = angle * Math.PI / 180; Id(id); add(position_x,position_y,width,height); return body; } private function Id(s:String):void{ bodyDef.userData = new Object(); bodyDef.userData.id = s; } public function square_dynamic_A(position_x:Number,position_y,width:Number,height:Number,angle:Number):b2Body{ bodyDef.type = b2Body.b2_dynamicBody; bodyDef.angle = angle * Math.PI / 180; add(position_x,position_y,width,height); return body; } private function draw(position_x:Number,position_y:Number,width:Number,height:Number,color:uint):void{ bodyDef.userData = new Sprite(); bodyDef.userData.graphics.lineStyle(3,0xffffff); addChild(bodyDef.userData); } private function add(position_x:Number,position_y:Number,width:Number,height:Number):void{ bodyDef.position.Set(position_x/30,position_y/30); boxShape.SetAsBox(width/30,height/30); fixtureDef.shape = boxShape; body = world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); } }