topical media & game development
student-mma-16-Clip2.mx
student-mma-16-Clip2.mx
[swf]
flex
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="64" height="78">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.controls.Image;
import mx.controls.Label;
public static var
GREEN:int = 0,
RED:int = 1,
BLUE:int = 2,
YELLOW:int = 3,
STATE_ERROR:int = -1,
STATE_INACTIVE:int = 0,
STATE_LOADING:int = 1,
STATE_IDLE:int = 2,
STATE_PLAYING:int = 3,
TYPE_NOLOOP:int = 0,
TYPE_LOOP:int = 1;
public var color:int;
public var url:String = "";
public var title:String = "";
public var type:int = TYPE_NOLOOP;
public var length:int = 1;
public var state:int = STATE_INACTIVE;
public var curBar:int = 0;
private var sound:Sound;
private var trans:SoundTransform;
private var soundChannel:SoundChannel;
private var progressBar:Shape;
private var img:Image;
public function init():void {
setImage();
setLabel();
createSound(url);
}
public function launch():void {
if(state == STATE_IDLE) {
play();
state = STATE_PLAYING;
img.alpha = 1;
}
}
public function play():void {
soundChannel = sound.play(0, 0, trans);
soundChannel.soundTransform = trans;
//soundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
}
private function soundComplete(e:Event):void {
if(type == TYPE_NOLOOP) {
stop();
}
else {
play();
}
}
public function stop():void {
soundChannel.stop();
state = STATE_IDLE;
img.alpha = 0.5;
}
public function setVolume(vol:Number):void {
if(vol < 0 || vol > 1) return;
trans.volume = vol;
soundChannel.soundTransform = trans;
}
private function setImage():void {
var src:String = "student-mma-16-img-final-";
switch(color) {
case RED: src += "loop-red.png"; break;
case BLUE: src += "loop-blue.png"; break;
case YELLOW: src += "loop-yellow.png";break;
default: src += "loop-green.png";
}
img = new Image();
img.source = src;
img.width = 64;
img.height = 78;
img.alpha = 0.5;
this.addChild(img);
}
private function setLabel():void {
var label:Label = new Label();
label.text = title;
label.x = 10;
label.y = 24;
label.styleName = "loopLabel";
label.truncateToFit = false;
label.width = 46;
label.setStyle("color", getColor(color));
this.addChild(label);
}
private function createSound(url:String):void {
var req:URLRequest = new URLRequest(url);
sound = new Sound();
createProgressBar();
sound.addEventListener(ProgressEvent.PROGRESS, updateProgress);
sound.addEventListener(Event.COMPLETE, loadComplete);
try {
sound.load(req);
state = STATE_LOADING;
trans = new SoundTransform();
}
catch(e:Error) {
state = STATE_ERROR;
}
}
public static function getColor(color:int):uint {
switch(color) {
case RED: return 0x7d0000;
case BLUE: return 0x00527d;
case YELLOW: return 0x7c7d00;
default: return 0x007d11;
}
}
private function createProgressBar():void {
progressBar = new Shape();
this.rawChildren.addChild(progressBar);
}
private function updateProgress(e:ProgressEvent):void {
var width:int = (e.bytesLoaded / e.bytesTotal) * 62;
progressBar.graphics.clear();
progressBar.graphics.beginFill(0xFFFFFF);
progressBar.graphics.drawRect(1, 65, width, 12);
}
private function loadComplete(e:Event):void {
state = STATE_IDLE;
progressBar.graphics.clear();
}
]]>
</mx:Script>
</mx:Canvas>
(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.