Wednesday, June 18, 2008

Loading XML and Parsing with AS3

This is what the XML doc looks like:
<images>
<pic>
<image>photos/pic17.jpg</image>
<thumb>thumbs/pic17.jpg</thumb>
<title>Kevin's Evil Face</title>
<desc>This is Kevin layin down some tracks. Boho Digitalia - February 2008</desc>
</pic>
<pic>
<image>photos/pic18.jpg</image>
<thumb>thumbs/pic18.jpg</thumb>
<title>Matt</title>
<desc>This is Matt singin some live bgv's</desc>
</pic>
<pic>
<image>photos/pic26.jpg</image>
<thumb>thumbs/pic26.jpg</thumb>
<title>The 4923</title>
<desc>Once upon a time, Matt wasn't in the band yet and Kevin played bass. Kilby Court - A Really Long Time Ago</desc>
</pic>
</images>

And this is how you load it into Flash and parse it:
var thumbs:Array = new Array();
var images:Array = new Array();
var titles:Array = new Array();
var descs:Array = new Array();

///////////////////////////////////
///// vvvvvv LOAD XML vvvvvv //////
//+++++++++++++++++++++++++++++++//
var loader:URLLoader = new URLLoader();
var xml:XML = new XML();
loader.addEventListener(Event.COMPLETE, loadXML);
loader.load(new URLRequest("images.xml"));
function loadXML(event:Event):void{
xml = new XML(event.target.data);
var total:Number = xml.pic.length();
for(var i:int = 0; i < total; i++){
thumbs[i] = xml.pic.thumb.text()[i];
images[i] = xml.pic.image.text()[i];
titles[i] = xml.pic.title.text()[i];
descs[i] = xml.pic.desc.text()[i];
}
loadThumbs();
}
//+++++++++++++++++++++++++++++++//
///////////////////////////////////
///////////////////////////////////

///// LOAD THE THUMBNAILS /////
function loadThumbs():void{
// do whatever you need here
trace(thumbs);
}

This will make 4 arrays (thumbs, images, titles, descs) and populates them with everything from your XML file. I still haven't taken the time to figure out how to parse it into a 2D array, so if anyone wants to tell me how to do it, that would rock.

Friday, June 13, 2008

As3 Music Player

I reprogrammed the player I made for my band's website in as3. Check it out:



Download Files

Somehow, and who knows how, the songs load super fast with the new sound object, which is awesome! I didn't even need to put a preloader on this one (which is good since I don't know how to make one yet). Some things about sound now are a little different but they aren't too annoying. The bar goes a little crazy when you click on it (to go to a different section in the song). It doesn't so that when I just export the swf. I'm hoping it's just some weird blogger thing.

Issues this covers: AS3, Actionscript 3.0, Sound, SoundChannel, SoundTransform, volume

Wednesday, June 11, 2008

Intro/Outro

This used to be a really simple task, but it's quite the ordeal in as3, but I figured it out:
View it Here
download fla's

It seems like there should be a way easier way of doing this.

Issues this covers: load movie, tween class, enter frame, target paths, variables
oh by the way, you have to do targets like this now:
MovieClip(this.parent.parent.parent).loadNextMovie(); //tell main timeline to call loadNextMovie function from loaded movie's timeline

Tuesday, June 10, 2008

new loadMovie

You don't need a container to load an external file anymore:

var loadMovie = new Loader();
addChild(loadMovie );
loadMovie .load(new URLRequest("page1.swf"));

but if you do want it to load into a certain movie or container, do it like this:
var loadMovie = new Loader();
container_mc.addChild(loadMovie );
loadMovie .load(new URLRequest("page1.swf"));

Apparently you can't just load a new movie in place of the new one anymore. You have to boot the old one out and then load in the new one:
container.removeChild(loadMovie);
loadMovie = new Loader();
container.addChild(loadMovie);
loadMovie.load(new URLRequest("page2.swf"));

Talking to the new movie's timeline is different too,
you'll have to cast it (it's called):
MovieClip(loadMovie.content).gotoAndStop("outro");

So far the new way to load a movie is the most annoying change about as3.

AS3 Tween Class

The tween class is pretty much the same as it was in AS2. When you import everything, you just have to change 'mx' to 'fl'. Also if you want to do a tween event you have to import TweenEvent:

import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;

var tw1:Tween = new Tween(object,"x",Strong.easeOut,start,end,time,true);

If you want to do an event (like the old onMotionFinished), it's a little different now. You have to create an event listener like this:
tw1.addEventListener(TweenEvent.MOTION_STOP, tweenStop);
function tweenStop(event:TweenEvent):void{
var tw2:Tween = new Tween(movieclip,"x",Back.easeOut,movieclip.x,300,1,true);
}

Monday, June 9, 2008

Tweening Experiments (Enter Frame)






CODE:
var myBrain:MovieClip = new MovieClip(); //create a new empty movie clip
var xPos:Number;
var yPos:Number;

box_mc.addEventListener(Event.ENTER_FRAME, rotate); //add new enter frame, call rotate function
stage.addEventListener(MouseEvent.CLICK, moveBox); //add new mouse event, call moveBox function

function rotate(event:Event):void{
box_mc.rotation += 8; //rotate box by 8 degrees 31 times/second
}

function moveBox(event:MouseEvent):void{
xPos = mouseX; //set xPos variable
yPos = mouseY; //set yPos veriable
myBrain.addEventListener(Event.ENTER_FRAME, moveIt); //add new enter frame, call moveIt
}

function moveIt(event:Event):void{
if(Math.abs(xPos-box_mc.x)<1 && Math.abs(yPos-box_mc.y)<1){ //when the box gets real close to destination...
myBrain.removeEventListener(Event.ENTER_FRAME, moveIt); //remove the enter frame that is on myBrain
box_mc.x = xPos; //snap box into position
box_mc.y = yPos; // " " "
}else{
box_mc.x += .1 * (xPos - box_mc.x); //simple easing formula
box_mc.y += .1 * (yPos - box_mc.y); //simple easing formula
}
}