Created
December 25, 2009 08:53
-
-
Save akisute/263566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.*; | |
import flash.filters.*; | |
import flash.events.*; | |
import flash.media.*; | |
public class MyPlayBass extends Sprite { | |
private var pointSpriteTop:Sprite; | |
private var pointSpriteBottom:Sprite; | |
private var lineSprite:Sprite; | |
private var amplitude:Number; | |
private var dampingRate:Number; | |
private var degree:Number; | |
private var sound:Sound; | |
private var soundChannel:SoundChannel; | |
public function MyPlayBass() { | |
// Requires "bassSE" class in library of a .fla file | |
sound = new bassSE(); | |
pointSpriteTop = new Sprite(); | |
// Looks better if draw functions are commented out | |
// pointSpriteTop.graphics.beginFill(0x666666); | |
// pointSpriteTop.graphics.drawCircle(0,0,2); | |
// pointSpriteTop.graphics.endFill(); | |
addChild(pointSpriteTop); | |
pointSpriteBottom = new Sprite(); | |
pointSpriteBottom.y=pointSpriteTop.y+250; | |
// Looks better if draw functions are commented out | |
// pointSpriteBottom.graphics.beginFill(0x666666); | |
// pointSpriteBottom.graphics.drawCircle(0,0,2); | |
// pointSpriteBottom.graphics.endFill(); | |
addChild(pointSpriteBottom); | |
lineSprite = new Sprite(); | |
lineSprite.buttonMode=true; | |
lineSprite.addEventListener(MouseEvent.MOUSE_DOWN, startAnimation); | |
addChild(lineSprite); | |
drawLine(); | |
} | |
private function startAnimation(event:Event):void { | |
soundChannel=sound.play(); | |
soundChannel.addEventListener(Event.SOUND_COMPLETE, stopAnimation); | |
amplitude=20.0; | |
dampingRate=1.0; | |
degree=0.0; | |
var blur:BlurFilter=new BlurFilter(2.0,0); | |
lineSprite.filters=[blur]; | |
lineSprite.addEventListener(Event.ENTER_FRAME, animateLine); | |
} | |
private function stopAnimation(event:Event):void { | |
dampingRate=0.9; | |
} | |
private function animateLine(event:Event):void { | |
var dampingFactor:Number = (soundChannel.leftPeak + soundChannel.rightPeak) / 2; | |
if (dampingFactor == 0) { | |
dampingFactor = 0.8; | |
} | |
degree = (degree + 90) % 360; | |
amplitude = 20.0 * dampingFactor; | |
drawLine(); | |
if (amplitude<1) { | |
lineSprite.filters=[]; | |
lineSprite.removeEventListener(Event.ENTER_FRAME, animateLine); | |
} | |
} | |
private function drawLine():void { | |
var ctx:Number=amplitude*Math.sin(degree*Math.PI/180); | |
var cty:Number = (pointSpriteBottom.y - pointSpriteTop.y) / 2; | |
lineSprite.graphics.clear(); | |
lineSprite.graphics.lineStyle(3.0, 0x666666); | |
lineSprite.graphics.moveTo(pointSpriteTop.x, pointSpriteTop.y); | |
lineSprite.graphics.curveTo(ctx, cty, pointSpriteBottom.x, pointSpriteBottom.y); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment