View Full Version : For Loops
alemieux34
January 31st, 2009, 12:03 PM
I've got some code to pull info from an XML file for an MP3 player. The way it's coded though, it only pulls the first record[0]. I'm trying to write for loops to pull other info into some dynamic text fields and have play buttons for each track in the XML file:
Here's the code:
function onLoadXML(ev:Event) {
try {
var myXML:XML = new XML(ev.target.data);
var list:XMLList = myXML.track;
displayDetails(list[0].@artist, list[0].@album, list[0].@title);
trace(displayDetails);
playSong(list[0].@source);
// populateList(list);
} catch (e:TypeError) {
trace("could not parse the XML");
trace(e.message);
}
}
function displayDetails(artist:String, album:String, title:String) {
trackInfo1.text = artist + "\t" + album + "\t" + title;
}
var song:SoundChannel;
var soundFactory:Sound = new Sound();
function playSong(id:String) {
var url:String = id;
var request:URLRequest = new URLRequest(url);
soundFactory.addEventListener(Event.COMPLETE, completeHandler);
soundFactory.addEventListener(Event.ID3, id3Handler);
soundFactory.addEventListener(IOErrorEvent.IO_ERRO R, ioErrorHandler);
soundFactory.addEventListener(ProgressEvent.PROGRE SS, progressHandler);
soundFactory.load(request);
// song = soundFactory.play();
play_btn.buttonMode = true;
play_btn.addEventListener(MouseEvent.CLICK, playTrack);
play_btn.addEventListener(MouseEvent.MOUSE_OVER, playOver);
play_btn.addEventListener(MouseEvent.MOUSE_OUT, playUp);
}
function playTrack(e:MouseEvent):void {
SoundMixer.stopAll();
song = soundFactory.play();
play_btn.enabled = false;
play_btn.mouseEnabled = false;
play_btn.gotoAndStop("off");
play_btn.alpha = .5;
}
I think it's these two lines that I need for loops for:
displayDetails(list[0].@artist, list[0].@album, list[0].@title);
playSong(list[0].@source);
What I was trying to use is:
for(var:Number = i; i <list.length; i++)
displayDetails(list[i].@artist, list[i].@album, list[i].@title);
}
This kind of works, but when I try to display the tracks into multiple dynamic text fields, it only shows the last track in the XML file.
cbeech
January 31st, 2009, 01:54 PM
here's one way to go about it - haven't really worked with XMLList much yet but it seems as this is one way to do it - however there are several other powerful features for sorting and targeting, i'm betting that there's a cleaner way ;)
ps - there seems to be several mentions in the docs about *not* using any reserve words in you naming of xml attributes, I'd move away from using 'title'
var xml:XML =
<trackData>
<tracks Artist='nameA' Album='albumA' Title='titleA' />
<tracks Artist='nameB' Album='albumB' Title='titleB' />
<tracks Artist='nameC' Album='albumC' Title='titleC' />
</trackData>;
for(var i=0; i<xml.child("*").length(); i++) {
displayDetails(xml.child(i).@Artist, xml.child(i).@Album, xml.child(i).@Title);
}
function displayDetails(artist:String, album:String, song:String) {
trace(artist + " : " + album + " : " + song);
}
alemieux34
January 31st, 2009, 02:59 PM
here's one way to go about it - haven't really worked with XMLList much yet but it seems as this is one way to do it - however there are several other powerful features for sorting and targeting, i'm betting that there's a cleaner way ;)
ps - there seems to be several mentions in the docs about *not* using any reserve words in you naming of xml attributes, I'd move away from using 'title'
var xml:XML =
<trackData>
<tracks Artist='nameA' Album='albumA' Title='titleA' />
<tracks Artist='nameB' Album='albumB' Title='titleB' />
<tracks Artist='nameC' Album='albumC' Title='titleC' />
</trackData>;
for(var i=0; i<xml.child("*").length(); i++) {
displayDetails(xml.child(i).@Artist, xml.child(i).@Album, xml.child(i).@Title);
}
function displayDetails(artist:String, album:String, song:String) {
trace(artist + " : " + album + " : " + song);
}
Thanks for the reply. I'm trying to implement it now, but after applying your code, nothing shows up in the dynamic text fields. I'm wondering if it's OK for the for loop to be in the try/catch statement?
alemieux34
January 31st, 2009, 03:17 PM
Oh, and by the way, I'm importing the XML from an external file, like this:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, onLoadXML);
loader.load(new URLRequest("tracks.xml"));
So, where you have xml.child, should it be something other than that?
Thanks.
here's one way to go about it - haven't really worked with XMLList much yet but it seems as this is one way to do it - however there are several other powerful features for sorting and targeting, i'm betting that there's a cleaner way ;)
ps - there seems to be several mentions in the docs about *not* using any reserve words in you naming of xml attributes, I'd move away from using 'title'
var xml:XML =
<trackData>
<tracks Artist='nameA' Album='albumA' Title='titleA' />
<tracks Artist='nameB' Album='albumB' Title='titleB' />
<tracks Artist='nameC' Album='albumC' Title='titleC' />
</trackData>;
for(var i=0; i<xml.child("*").length(); i++) {
displayDetails(xml.child(i).@Artist, xml.child(i).@Album, xml.child(i).@Title);
}
function displayDetails(artist:String, album:String, song:String) {
trace(artist + " : " + album + " : " + song);
}
alemieux34
January 31st, 2009, 03:34 PM
Using this:
for (var i=0; i<list.length(); i++) {
displayDetails(list[i].@artist, list[i].@album, list[i].@song);
}
I got it to work, but now I can't get the separate pieces of info to show up in separate dynamic text fields, one for each track. Any ideas?
cbeech
January 31st, 2009, 06:12 PM
should be of course just fine to import the xml - i just had that in the code as sample to work with.
glad you got it to work that way. how are you assigning the text then? and you've embedded the font into the field right? ;)
alemieux34
February 2nd, 2009, 09:31 AM
This function:
function displayDetails(artist:String, album:String, title:String) {
trace(artist + " : " + album + " : " + song);
fullInfo.text += artist + " : " + album + " : " + song + "\n";
}
I'll emed fonts for sure, just want to get this thing working first.
should be of course just fine to import the xml - i just had that in the code as sample to work with.
glad you got it to work that way. how are you assigning the text then? and you've embedded the font into the field right? ;)
cbeech
February 2nd, 2009, 03:14 PM
if the trace is working and the 'field' is not - you need to embed the fonts into you dynamic textfield instance - you should always do so when you create the instance - with the field selected, click the embed button in the properties panel.
alemieux34
February 3rd, 2009, 09:24 AM
Actually, I'm using a generic face _sans for the font, so that's automatically embedded or at least, I can't embed it.
if the trace is working and the 'field' is not - you need to embed the fonts into you dynamic textfield instance - you should always do so when you create the instance - with the field selected, click the embed button in the properties panel.
cbeech
February 3rd, 2009, 10:43 AM
ok super ;) that's known as a 'device' font - and you are correct you shouldn't need to embed it. however keep in mind (and i realize that this is not the issue atm) that when using a device font one can't scale, rotate or mask the field - just handy tidbit for you.
but i found the problem. you still have 'title' in the parameters and 'song' in the string(s) - change out the param and it should fire up.
alemieux34
February 5th, 2009, 10:51 AM
Thanks for the advice on device fonts. Eventually, when I get this darn thing to work, I'll move to real fonts and embed them.
I've move on to a class-based approach for this project. I've got a few classes going now and I think the structure of my XML file is a problem. I'm looking into the difference between XMLList and XML.
ok super ;) that's known as a 'device' font - and you are correct you shouldn't need to embed it. however keep in mind (and i realize that this is not the issue atm) that when using a device font one can't scale, rotate or mask the field - just handy tidbit for you.
but i found the problem. you still have 'title' in the parameters and 'song' in the string(s) - change out the param and it should fire up.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.