PDA

View Full Version : XML blues. Sequentially parsing array.



kbd
September 22nd, 2007, 04:24 AM
:skull:Hello,
I'm trying to convert a test file from AS2 to AS3. All I'm looking to do with this is to get my buttons to sequentially go through the titles in my XML file. Click NEXT BUTTON it takes you forward..Click PREV button it takes you back etc. etc. If you are on the last ( or first ) item & a button is pressed it will proceed to the first item in the array & visa versa.
I have no idea what I am doing anymore but I will post my LSD spider web actionscript debauchery in the event that someone wants to help a brotha out.


var currentlyPlaying:Number = 0;
var totalNumber:Number = 0;
/////////////////
nextButton.addEventListener(MouseEvent.CLICK, nextButtonclick);
prevButton.addEventListener(MouseEvent.CLICK, prevButtonclick);
nextButton.buttonMode = true;
prevButton.buttonMode = true;
/////////////////

var arTitle:Array = new Array(); // zero clue how to access the title nodes in array

/////////////////

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("media.xml"));


function LoadXML(e:Event):void {

xmlData = new XML(e.target.data);
ParseMedia(xmlData);

}

function ParseMedia(mediaInput:XML):void {

var titleList:XMLList = mediaInput.theMedia.title;

for (var i:int = 0; i < xmlData.length(); i++){
var titleElement:XML = titleList[currentlyPlaying];
myTitle.text=titleList[currentlyPlaying];
}
}

function nextButtonclick(event:MouseEvent):void {
trace("next button clicked");
nextItem();
}
function prevButtonclick(event:MouseEvent):void {
trace("prev button clicked");
prevItem();

}

function nextItem() {
if (currentlyPlaying<(totalNumber-1)) {
currentlyPlaying++;
} else {
currentlyPlaying = 0;
}
populateText();
}
function prevItem() {
if (currentlyPlaying == 0) {
currentlyPlaying = totalNumber-1;
} else {
currentlyPlaying--;
}
populateText();
}

function populateText() {
trace("THIS IS THE MEDIA:"+currentlyPlaying);
}
populateText();

stop();XML

<medias>
<theMedia type="mp3 file">
<title>This is the title</title>
<artist>This is the artist</artist>
<thefile>file.mp3</thefile>
</theMedia>

<theMedia type="mp3 file">
<title>This is the title2</title>
<artist>This is the artist2</artist>
<thefile>file2.mp3</thefile>
</theMedia>
</medias>

ijmacdowell
September 24th, 2007, 01:07 PM
first things first, you need to import the classes to handle the Mouse events and so on...
import flash.display.Sprite;
import flash.events.*;
import flash.display.MovieClip;
import flash.filters.DropShadowFilter;
import flash.filters.BevelFilter;
import flash.text.TextFormatAlign;
import flash.text.TextField;
import flash.text.TextFormat;

stuff like that...
then you need to make sure that the instance name are refrenced correctly

kbd
September 24th, 2007, 06:45 PM
This isn't a class file. Do I still need to do that?

greggreg
September 25th, 2007, 01:57 AM
first off, no you dont need the imports if your putting the code directly on the timeline.

second, you got it mostly right just forgot a few things, here goes..

a) you never updated the 'totalNumber' var to actually hold the total number, therefore no iteration could take place.

b) you needed to declare the 'titleList' var outside the parse function as it gets stuck in the functions scope

c) the display text never gets updated after a button is clicked, so even if everything else was fine youd never see the change.

in any case heres the working updated code:




var currentlyPlaying:Number = 0;
var totalNumber:Number = 0;
/////////////////
nextButton.addEventListener(MouseEvent.CLICK, nextButtonclick);
prevButton.addEventListener(MouseEvent.CLICK, prevButtonclick);
nextButton.buttonMode = true;
prevButton.buttonMode = true;
/////////////////

var titleList:XMLList = new XMLList();
/////////////////

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("media.xml"));


function LoadXML(e:Event):void {

xmlData = new XML(e.target.data);
ParseMedia(xmlData);

}

function ParseMedia(mediaInput:XML):void {

titleList = mediaInput.theMedia.title;
trace(titleList);

//var myTitle:String;
totalNumber = xmlData.theMedia.length();

for (var i:int = 0; i < xmlData.length(); i++) {
var titleElement:XML = titleList[currentlyPlaying];
myTitle.text=titleList[currentlyPlaying];
}
}

function nextButtonclick(event:MouseEvent):void {
trace("next button clicked");
nextItem();
}
function prevButtonclick(event:MouseEvent):void {
trace("prev button clicked");
prevItem();

}

function nextItem() {
if (currentlyPlaying<(totalNumber-1)) {
currentlyPlaying++;
} else {
currentlyPlaying = 0;
}
populateText();
}
function prevItem() {
if (currentlyPlaying == 0) {
currentlyPlaying = totalNumber-1;
} else {
currentlyPlaying--;
}
populateText();
}

function populateText() {
trace("THIS IS THE MEDIA:"+currentlyPlaying);
myTitle.text=titleList[currentlyPlaying];

}


stop();