Monday, April 14, 2008

Problems loading a local XML file in AIR on a Mac

I would never believe that I would curse a Mac -- and it's really not Macs fault! In order to let a user save the photos on this DVD project, I'm trying to build it with AIR and then port to Shu -- but for some reason I am unable to get an AIR file to access a local xml file from the same directory. Here is how I've been trying to do it:

var xmlLoader:URLLoader = new URLLoader();

var file:File = flash.filesystem.File.applicationDirectory;

file = file.resolvePath("my.xml");

var xmlPath:URLRequest = new URLRequest(file.url);

try {
xmlLoader.load(xmlPath);
} catch (error:Error) {
trace("Unable to load requested document.");
}


Works perfectly on Windows but I get the #2032 error on Mac. If I can't get this fixed today then I'll probably be forced to use Zinc instead...

Sunday, April 6, 2008

Loading too many photos too fast!

On this project we have to load over 200 photos to the screen before the app should start. I was using a for loop to instantiate the Loaders, but kept finding sporadic results in loading. Sometimes all the photos would come in and other times only some of them would. Anyway, although I have nothing to prove it, I think it just was too much for flash to load so many things at once, so it would drop some as needed. In any event I decided to use a timer instead to do the loading:
public function addPhotosByTimer():void{
// the photo paths are stored in an array
var photoArrayLen:int = myPhotoArray.length;
// trigger the timer only for the number of items in the array
// and add a photo every 1oth of the sec
var photoAddTimer:Timer = new Timer(100,photoArrayLen);
var j:int = 0;
photoAddTimer.addEventListener(TimerEvent.TIMER,addPhoto);
photoAddTimer.start();

function addPhoto(evt:Event):void{
var photoLoader = new Loader();
var thisPath:String = thisSectPhotoArray[j];
photoLoader.load(thisPath);
j++;
}
}
So far it seems to work fine...

Cannot create property ColorTransform on flash.geom.Transform

Ok, here was a weird one. I was trying to change the color of a movieclip at runtime -- which could be done with setRGB() in actionscript 2. So I did this to match the new AS3 approach:

import flash.geom.ColorTransform;

var myCT:ColorTransform = new ColorTransform();
myCT.color = 0x8e5823;

myMovieClip.transform.ColorTransform = myCT;
But when I run this I got this error:

"Cannot create property ColorTransform on flash.geom.Transform"

After pulling my hair out (just for a bit) I found that I had capitalized the colorTranform property of the MovieClip. Once I changed the last line to this:
myMovieClip.transform.colorTransform = myCT;
...it worked fine. But since I didn't get any Google result for that error I figured I would put this in.

Simple mistake but very annoying ;-)

Play mp3 files in AS3

Here is a method that can be called to play a an MP3 file. If there is a current file playing it stops before starting the next one. It should be noted that once you call the play() method on a Sound you cannot load another mp3 into it -- you need to create a new one.

I should probably turn this into a class to make it easier for reuse:

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;

private var _soundChannel:SoundChannel;
private var _sound:Sound;

soundVolume = new SoundTransform();
soundVolume.volume = 1;

public function getVolume():SoundTransform{
return soundVolume;
}

public function playSound(url:String):void{
var request:URLRequest = new URLRequest(url);
if(_soundChannel!=null){
_soundChannel.stop();
_sound = null;
}
_sound = new Sound();
_sound.load(request);
_soundChannel = _sound.play();
_soundChannel.soundTransform = getVolume();
}

playSound("myfile.mp3");

Creating a toggling mute button in AS3

Here is some code to create a toggle mute button for sounds in AS3

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;

private var muted:Boolean = false;
private var soundFactory:Sound;
private var soundClip:SoundChannel;
private var soundVolume:SoundTransform;
private var url:String = "myclip.mp3";
private var request:URLRequest = new URLRequest(url);


// create a new SoundTransform obj
soundVolume = new SoundTransform();
soundVolume.volume = 1;

// now create a new sound obj
soundFactory = new Sound();
// now load the mp3
soundFactory.load(request);
// and play it
soundClip = soundFactory.play();

// now set the volume of the clip --
//you need to do this AFTER the clip is playing
soundClip.soundTransform = soundVolume;

// here is an eventHandler that will mute and unmute the volume
private function toggleVolumeListener(evt:Event):void{
//toggle the boolean
_muted = !_muted;
if(_muted){
// first set the var for use with other sound clips
soundVolume.volume = 0;
// now reset the soundTransform obj of the soundClip
soundClip.soundTransform = soundVolume;
}else{
soundVolume.volume = 1;
// now reset the soundTransform obj of the soundClip
soundClip.soundTransform = soundVolume;
}
}

gotoAndStop in AS3

Very important changes in AS3 when trying to access objects in a movieClip timeline using gotoAndStop(). Here is a great blog entry outlining this change:

http://www.kirupa.com/forum/showpost.php?p=2113726&postcount=358