package { import away3d.cameras.HoverCamera3D; import away3d.containers.View3D; import away3d.materials.BitmapMaterial; import away3d.materials.ColorMaterial; import away3d.primitives.Cube; import away3d.primitives.RegularPolygon; import away3d.core.utils.Cast; import flash.display.Bitmap; import flash.display.Sprite; import flash.events.Event; import flash.filters.BlurFilter; import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundMixer; import flash.net.URLRequest; import flash.utils.ByteArray; import flash.utils.Timer; [SWF(width="500", height="400", frameRate="60", backgroundColor="#ffffff")] public class Basic08_cube_music extends Sprite { private var cam:HoverCamera3D; private var lastKey:uint; private var keyIsDown:Boolean = false; private var view:View3D; private var cover:Cover; private var numCubes:Number = 16; // Set depending on speed of computer. adjust amplitude accordingly private var amplitude:Number = 70; private var cubes:Array; private var cubesMirrored:Array; private var cubeColors:Array; private var mp3:Sound; private var soundBytes:ByteArray; private var soundPlaying:Boolean = false; private var soundValues:Array; private var pausePosition:int = 0; private var channel:SoundChannel; private var highlightFilter:Array; private var maxLev:Number = 0; private var t:Timer; [Embed (source="resources/greyish.jpg")] private var greyTexture:Class; public function Basic08_cube_music() { // init objects cubes = new Array(); cubesMirrored = new Array(); cubeColors = new Array(); soundBytes = new ByteArray(); soundValues = new Array(); // setup and load mp3 mp3 = new Sound(); mp3.load(new URLRequest("/articlefiles/away3d/resources/redrum.mp3")); // Note: you will have to change this to point to one of your own mp3 files channel = new SoundChannel(); // prefill arrays with zero's for(var p:int = 0; p soundValues[i]){ newSoundValues[i] = newValues[i]; cubeColors[i] = 128; currentCube.filters = highlightFilter; currentCubeMirror.filters = highlightFilter; } else { // If lower, just keep fading var dampedValue:Number = soundValues[i]-3; if(dampedValue<1){ dampedValue = 1; } cubeColors[i] -= 10; newSoundValues[i] = dampedValue; currentCube.filters = null; currentCubeMirror.filters = null; } // Adjust cube and mirror cube height currentCube.height = newSoundValues[i]; currentCube.y = newSoundValues[i]/2; currentCubeMirror.height = newSoundValues[i]; currentCubeMirror.y = newSoundValues[i]/2*-1; // Adjust color var adjustedColor:uint = cubeColors[i]; var colString:String; var colStringMirror:String; var increase:Number = 1.7; // how much whiter shall the "reflected" cubes be if(i 255){ n = 255;} if(n < 1){ n = 1;} if(n < 16){ s = "0"+n.toString(16); } else { s = n.toString(16); } return s; } // Sound events triggered by the cover private function playSound(e:Event):void { if(!soundPlaying){ soundPlaying = true; channel = mp3.play(pausePosition); if( !channel.hasEventListener(Event.SOUND_COMPLETE) ){ channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); } } } private function stopSound(e:Event):void { pausePosition = channel.position; channel.stop(); soundPlaying = false; } // restart the mp3 when finished playing private function soundCompleteHandler(e:Event):void { pausePosition = 0; soundPlaying = false; playSound(e); } } }