flash on 2015-3-24

by Cheshir
♥0 | Line 105 | Modified 2015-04-04 02:36:00 | MIT License
play

ActionScript3 source code

/**
 * Copyright Cheshir ( http://wonderfl.net/user/Cheshir )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/z9qU
 */

package {
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            var vaweC:Sprite = new Sprite();
            vaweC.x = 100;
            addChild(vaweC);
            
            var corA:Cord = new Cord(stage.stageHeight-10, 124, vaweC);
            var corB:Cord = new Cord(stage.stageHeight-10, 90, vaweC);
            var corC:Cord = new Cord(stage.stageHeight-10, 64, vaweC);
        //    var corD:Cord = new Cord(stage.stageHeight-10, 36);
            corA.x = 10;
            corB.x = 40;
            corC.x = 70;
        //    corD.x = 100;
            addChild(corA);
            addChild(corB);
            addChild(corC);
        //    addChild(corD);
        }        
    }
}
import flash.events.SampleDataEvent;
import flash.media.SoundChannel;
import flash.media.Sound;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
Class {
    class Cord extends Sprite {
        private var heigthCord:Number;
        private var toPlay:Sound;
        private var toPlayNumber:Number;
        private var myChannel:SoundChannel = new SoundChannel();
        private var toDraw:Sprite;
        public function Cord(height:Number, toPlay:Number=64, graphics:Sprite=null){
            this.heigthCord = height;
            this.toPlay = new Sound();
            this.toPlayNumber = toPlay;
            this.toDraw = graphics;
            this.toPlay.addEventListener(SampleDataEvent.SAMPLE_DATA, sineWaveGenerator);
            this.graphics.beginFill(0xcccccc);
            this.graphics.drawRect(0,0,20,heigthCord);
            this.graphics.endFill();
            this.graphics.lineStyle(1);
            this.graphics.moveTo(10,0);
            this.graphics.lineTo(10,heigthCord);
            addEventListener(MouseEvent.MOUSE_OVER, animateStart);
            addEventListener(MouseEvent.MOUSE_UP, animateStop);
            addEventListener(MouseEvent.MOUSE_OUT, animateStop);
        //    addEventListener(Event.ENTER_FRAME, animateLoop);
            this.buttonMode = true;
        }
        private var vaweVal:Number;
        private function sineWaveGenerator(event:SampleDataEvent):void{
            if(toDraw){
                toDraw.graphics.clear();
                toDraw.graphics.lineStyle(1);
                toDraw.graphics.moveTo(0,200);
            }
            for ( var c:int=0; c<8192; c++ ) {    // 8192
                  var v:Number = Math.sin(Number(c+event.position)/(toPlayNumber-soundY)*10);
                  v+=Math.cos(event.position/10000);//+Math.atan2(c,event.position);
                  vaweVal = v;
                  event.data.writeFloat(vaweVal);
                  event.data.writeFloat(vaweVal);
                  if(toDraw && c<300){
                      toDraw.graphics.lineTo(c,vaweVal*10+200);
                  }
            }
        }

        private var animate:Boolean = false;
        private var animateX:Number = 9.5;
        private var soundY:Number = 0;
        private function animateStart(e:MouseEvent):void { animate=true; soundY=e.localY; myChannel = toPlay.play(); addEventListener(Event.ENTER_FRAME, animateLoop);}
        private function animateStop(e:MouseEvent):void { animate=false; myChannel.stop();}
        private function animateLoop(e:Event):void {
            if(animate){
                this.graphics.clear();
                this.graphics.beginFill(0xcccccc,0);
                this.graphics.drawRect(0,0,20,heigthCord);
                this.graphics.endFill();
                this.graphics.beginFill(0);
                this.graphics.drawCircle(10,soundY,6);
                this.graphics.endFill();
                this.graphics.lineStyle(1);
                this.graphics.moveTo(10,0);
                this.graphics.lineTo(10,soundY);
                this.graphics.moveTo(animateX,soundY);
                this.graphics.lineTo(animateX,heigthCord);
                if(animateX==9.5){
                 animateX = 10.5;
                } else {
                 animateX = 9.5;
                }
            } else {
                this.graphics.beginFill(0xcccccc,0);
                this.graphics.drawRect(0,0,20,heigthCord);
                this.graphics.endFill();
                this.graphics.lineStyle(1);
                this.graphics.moveTo(10,0);
                this.graphics.lineTo(10,heigthCord);
                removeEventListener(Event.ENTER_FRAME, animateLoop);
            }
        }
    }
}