PDA

View Full Version : Help with XML variables -- arrays, functions, etc



hgfl
May 18th, 2005, 06:12 PM
I have been working on a Flash movie that loads variable data from an external XML file and dynamically displays the item names in a menu which may then be clicked to display other dynamically-loaded content that corresponds to the menu item that was clicked.

I have now come to a standstill in the project as there is something I'd like to achieve but simply cannot work out how. This is my first attempt at working with XML in Flash and one of my early attempts at using variables.

What I'm trying to achieve is; once the user has viewed the item's content, they can simply click "next" and "previous" buttons to display all of the content for the next or previous items in the XML document.

I know it probably sounds simple and I'm sure some of you could achieve this in 5 minutes (!) but I can't get my head around some of the coding. I assume previousSibling and nextSibling would come into play here as well as some sort of functions which access arrays of data for the previous and next items, but I'm just not sure how to correctly put these things to use.

Rather than post my code and example images of the stage up here on the forums, I've created an html page here: http://www.basemedia.com.au/hosted/help (http://www.basemedia.com.au/hosted/help)

Please, if you think you can be of any help, check it out. I would really appreciate it.
Thanks.

East High
May 18th, 2005, 08:44 PM
try this link (http://www.kirupa.com/developer/mx2004/xml_flash_photogallery.htm)

hgfl
May 18th, 2005, 10:23 PM
Thanks for that... but I'm still having a little prob with something else I'm trying to achieve...

I've downloaded the files for the XML photo gallery and am now trying to modify it so that it has a menu rather than just loading up the first image as soon as the movie is played. I've done this by adding a new Scene, named "Scene 1" and renamed the original Scene to "Scene 2" (the one with the content).

In the first scene, I've placed a movieClip with instance name "menu_mc". I've also moved the ActionScript from Scene 2 into Scene 1 (with the menu) and had added some extra stuff to the ActionScript so that the "Description" names are grabbed out of the XML document and placed as separate items on the menu.

What I'd like is to have it so you may click a button and you'll be taken to "Scene 2" and the corresponding image and text are displayed. Here's the modified code I have in "Scene 1":


stop();
function loadXML(loaded) {
if (loaded) {
xmlNode = this.firstChild;
image = [];
description = [];
total = xmlNode.childNodes.length;
///
var item_spacing = 28;
var item_count = 0;
///
for (i=0; i<total; i++) {
image[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
description[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
///
var item_mc = menu_mc.attachMovie("menu_item", "item"+item_count, item_count);
item_mc._y = item_count*item_spacing;
item_count++;
myItem = item_mc.main_btn;
item_mc.menuitem_txt.text = description[i];
myItem.onRelease = displayImage;//goes to function below in which I'd like to pass variables according to the button pressed
};
//}
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("images.xml");
p = 0;
this.onEnterFrame = function() {
filesize = picture.getBytesTotal();
loaded = picture.getBytesLoaded();
preloader._visible = true;
if (loaded != filesize) {
preloader.preload_bar._xscale = 100*loaded/filesize;
} else {
preloader._visible = false;
if (picture._alpha<100) {
picture._alpha += 10;
}
}
};
function nextImage() {
if (p<(total-1)) {
p++;
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(image[p], 1);
desc_txt.text = description[p];
picture_num();
}
}
}
function prevImage() {
if (p>0) {
p--;
picture._alpha = 0;
picture.loadMovie(image[p], 1);
desc_txt.text = description[p];
picture_num();
}
}
function displayImage() {
//if (loaded == filesize) {
//somehow here I'll need to pass the variables onto "Scene 2" so that when
//the movie goes to "Scene 2", it knows what image and text to display that
//corresponds to the button just pressed at "Scene 1"
picture._alpha = 0;
picture.loadMovie(image[0], 1);
desc_txt.text = description[0];
picture_num();
gotoAndPlay("Scene 2");
//}
}
function picture_num() {
current_pos = p+1;
pos_txt.text = current_pos+" / "+total;
}


The menu displays the description text perfectly, but I'm not too sure how I can pass the variables corresponding to the button clicked onto "Scene 2" for it to display that variable data.Can anyone help?
Cheers