topical media & game development
student-mma-16-Final.mx
student-mma-16-Final.mx
[swf]
[flash]
flex
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#414141, #414141]" xmlns:ns1="*"
frameRate="25" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.effects.Sequence;
import de.popforge.format.wav.WavFormat;
import de.popforge.audio.output.Sample;
import mx.controls.Label;
import mx.controls.Alert;
import de.popforge.audio.output.SoundFactory;
import de.popforge.audio.output.Audio;
private var clips:Array = new Array();
private var progressBar:Shape; //progress bar for bar progress
private var sequencer:student_mma_16_Sequencer;
private var b:int = 1; //beat counter (resets after each bar)
private var time:Number;
private var barTimer:Timer;
private var beatTimer:student_mma_16_MyTimer;
private var tickTimer:Timer;
private var c:int = 0;
private var draggingClip:student_mma_16_Clip;
private function init():void {
loadSoundBank();
progressBar = new Shape();
field.rawChildren.addChild(progressBar);
sequencer = new student_mma_16_Sequencer();
sequencer.onInit = startBarProgressTimer;
}
private function startBarProgressTimer():void {
barTimer = new Timer(185, 1); //approx buffer fill time delay
barTimer.addEventListener(TimerEvent.TIMER, barProgressTimer);
barTimer.start();
}
private function barProgressTimer(e:TimerEvent):void {
beatTimer = new student_mma_16_MyTimer(480, 0, beat);
beatTimer.start();
}
private function beat():void {
//update bar progress bar
var width:int = Math.round((field.width - 2) / 4 * b);
progressBar.graphics.clear();
progressBar.graphics.beginFill(0xFFFFFF);
progressBar.graphics.drawRect(1, 434, width, 12);
b++;
if(b == 5) {
b = 1;
}
}
private function loadSoundBank():void {
var myLoader:URLLoader = new URLLoader();
myLoader.addEventListener(Event.COMPLETE, soundBankLoaded);
myLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
try {
myLoader.load(new URLRequest("student-mma-16-SoundBank.xml"));
}
catch(e:Error) {
Alert.show("Unable to load sound bank: " + e.message);
}
}
private function ioErrorHandler(e:Event):void {
Alert.show("Unable to load sound bank");
}
private function soundBankLoaded(e:Event):void {
var xml:XML = new XML(e.target.data);
xml.normalize();
var curCat:HBox;
var catLabel:Label;
var curClip:student_mma_16_Clip;
var clipLabel:Label;
for(var i:int = 0; i < xml.category.length(); i++) {
//create category in clip menu
curCat = new HBox();
curCat.percentWidth = 100;
curCat.styleName = "clipMenuHeader";
curCat.setStyle("backgroundColor", student_mma_16_Clip.getColor(i));
catLabel = new Label();
catLabel.text = xml.category[i].@name;
catLabel.styleName = "clipMenuHeaderLabel";
curCat.addChild(catLabel);
clipMenu.addChild(curCat);
//loop through its sounds
for(var c:int = 0; c < xml.category[i].sound.length(); c++) {
//create sound clip
curClip = new student_mma_16_Clip();
curClip.title = xml.category[i].sound[c].title;
curClip.color = i;
curClip.type = xml.category[i].sound[c].type;
curClip.url = "student-mma-16-sounds-" + xml.category[i].sound[c].file;
curClip.length = xml.category[i].sound[c].length;
clips.push(curClip);
//add sound label
clipLabel = new Label();
clipLabel.text = curClip.title;
clipLabel.id = "clipLabel" + (clips.length - 1);
clipLabel.buttonMode = true;
clipLabel.useHandCursor = true;
clipLabel.mouseChildren = false;
clipLabel.styleName = "clipLabel";
clipLabel.setStyle("color", student_mma_16_Clip.getColor(curClip.color));
clipLabel.addEventListener(MouseEvent.CLICK, addClip);
clipMenu.addChild(clipLabel);
}
}
}
private function addClip(e:MouseEvent):void {
var clipIndex:int = e.target.id.substr(9);
var clip:student_mma_16_Clip = clips[clipIndex];
if(clip == null || clip.state != student_mma_16_Clip.STATE_INACTIVE) return;
clip.init();
clip.lastParent = "prep";
clip.addEventListener(MouseEvent.MOUSE_DOWN, startClipDrag);
prep.addChild(clip);
}
private function clipToField(clip:student_mma_16_Clip):void {
field.addChild(clip);
clip.x += prep.x - field.x;
clip.y += prep.y - field.y;
adjustClipPosition(clip);
clip.lastParent = "field";
clip.start(sequencer);
setClipVolume(clip);
}
private function clipToPrep(clip:student_mma_16_Clip):void {
prep.addChild(clip);
clip.lastParent = "prep";
}
private function startClipDrag(e:MouseEvent):void {
var clip:student_mma_16_Clip = e.currentTarget as student_mma_16_Clip;
if(clip.state < student_mma_16_Clip.STATE_IDLE) return;
clip.lastX = clip.x;
clip.lastY = clip.y;
clip.startDrag();
clip.addEventListener(MouseEvent.MOUSE_UP, stopClipDrag);
if(clip.lastParent == "field")
clip.addEventListener(MouseEvent.MOUSE_MOVE, moveClip);
}
private function stopClipDrag(e:MouseEvent):void {
var clip:student_mma_16_Clip = e.currentTarget as student_mma_16_Clip;
clip.stopDrag();
clip.removeEventListener(MouseEvent.MOUSE_UP, stopClipDrag);
var clipY:Number = clip.y + clip.height / 2; //center of clip
var clipX:Number = clip.x + clip.width / 2;
var prepMargin:Number = prep.y - field.y + field.height;
if(clip.lastParent == "prep") {
var fieldBottom:Number = field.y + field.height - prep.y;
var fieldTop:Number = fieldBottom - field.height;
var fieldRight:Number = field.x + field.width - prep.x;
var fieldLeft:Number = fieldRight - field.width;
var margin:Number = Math.abs(fieldBottom);
var onField:Boolean = clipY < fieldBottom && clipY > fieldTop
&& clipX > fieldLeft && clipX < fieldRight;
var onPrep:Boolean = clipY > 0 - margin && clipY < prep.height + margin
&& clipX > 0 - margin && clipX < prep.width + margin;
if(onField) {
clipToField(clip);
}
else if(onPrep) {
//snap back
clip.x = clip.lastX;
clip.y = clip.lastY;
}
else {
//if clip is out of prep and field, remove it
prep.removeChild(clip);
clip.remove();
}
}
else if(clip.lastParent == "field") {
var prepTop:Number = prep.y - prepMargin - field.y;
var prepBottom:Number = prepTop + prep.height + 2 * prepMargin;
var prepLeft:Number = prep.x - prepMargin - field.x;
var prepRight:Number = prepLeft + prep.width + 2 * prepMargin;
var onField:Boolean = clipY > 0 && clipY < field.height
&& clipX > 0 && clipX < field.width;
var onPrep:Boolean = clipY > prepTop && clipY < prepBottom
&& clipX > prepLeft && clipX < prepRight;
if(onField)
adjustClipPosition(clip);
else{
clip.stop();
clipToPrep(clip);
}
}
}
private function setClipVolume(clip:student_mma_16_Clip):void {
if(clip.x >= 0 && clip.x <= field.width - clip.width && clip.y >= 0 && clip.y <= field.height - clip.height)
clip.setVolume(1 - clip.y / (field.height - clip.height));
}
private function moveClip(e:MouseEvent):void {
var clip:student_mma_16_Clip = e.currentTarget as student_mma_16_Clip;
if(clip.state == student_mma_16_Clip.STATE_PLAYING) {
setClipVolume(clip);
}
}
private function adjustClipPosition(clip:student_mma_16_Clip):void {
if(clip.x < 0) clip.x = 0;
if(clip.x + clip.width > field.width) clip.x = field.width - clip.width;
if(clip.y < 0) clip.y = 0;
if(clip.y + clip.height > field.height) clip.y = field.height - clip.height;
}
]]>
</mx:Script>
<mx:Style source="student-mma-16-styles.css"/>
<mx:Grid height="571">
<mx:GridRow width="100%" height="100%">
<mx:GridItem width="900" height="100%" backgroundImage="@Embed(source='student-mma-16-img-final-app2.png')">
<mx:Canvas width="100%" height="100%">
<mx:Canvas x="0" y="0" width="900" height="447" id="field">
<ns1:student_mma_16_FrequencySpectrum width="896" height="447" alpha="0.3">
</ns1:student_mma_16_FrequencySpectrum>
</mx:Canvas>
<mx:HBox x="20" y="474" width="859" height="78" id="prep">
</mx:HBox>
</mx:Canvas>
</mx:GridItem>
<mx:GridItem width="242" height="100%" backgroundImage="@Embed(source='student-mma-16-img-final-menu-clips.png')" paddingTop="80" paddingLeft="20" paddingRight="29">
<mx:VBox height="100%" width="100%" id="clipMenu">
</mx:VBox>
</mx:GridItem>
</mx:GridRow>
</mx:Grid>
</mx:Application>
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.