PDA

View Full Version : Have Flash check XML for data


mattGSSI
10-20-2009, 05:03 PM
Hi everyone,

I've just been given an edit to do to some existing code and I'm lost. I don't know much if anything on actionscript 3, and even my AS2 knowledge is limited.

Essentially, this code basically creates a slideshow with a 'Rollover' button. When you hover over the rollover button, it displays another image defined in the XML.

Edit: Forgot to mention the issue! Duh... anyway, at the moment the code always displays the 'rollover' button. I'd like it to be able to check the XML to see if the <over> tag is defined, and then show/hide the button appropriately.

This should be possible, because it's able to tell when the currently displayed image is first or last in the list (it hides the left/right arrow buttons respectively)

I don't even know where to start, so I'm just going to paste all the code that looks relevant and hope for the best.

Sorry I can't give you an example of how it works now. Hope you can help with just the code. Any help would be appreciated!

(The rollover button is named 'over' in the code)


//--- XML LOADER
//================================================== ================================================== ==============
xmlLoader.addEventListener(Event.COMPLETE, xmlParser);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, throwIOError);

function xmlParser(e:Event):void {
try
{
xmlData = new XML(e.target.data);

// populate object
slider_obj.itemNum = xmlData.item.length();
slider_obj.imagePath = xmlData.attribute("imagePath");
slider_obj.swfPath = xmlData.attribute("swfPath");
slider_obj.itemType = xmlData.item.attribute("type");
slider_obj.itemURL = xmlData.item.attribute("url");
slider_obj.itemWindow = xmlData.item.attribute("window");
slider_obj.itemImage = xmlData.item.image;
slider_obj.itemOver = xmlData.item.over;

loadButtons();
loadImages();
startTimer();
}
catch (e:Error)
{
throwError(e.message);
return;
}
}



//================================================== ================================================== ==============
//--- INIT MC
//================================================== ================================================== ==============
function initMC():void {
// load xml
startPanel = this.loaderInfo.parameters.start;
if (startPanel) {
startPanel = Number(startPanel); // parameter is 0-based
} else {
startPanel = 0;
}
if (startPanel < 0) { startPanel = 0; }
trace("startPanel: " + startPanel );
xmlLoader.load(new URLRequest(xmlPath));
}


//================================================== ================================================== ==============
//--- IMAGE LOADER
//================================================== ================================================== ==============
var itemPath:String;


function loadImages():void {
var loadorder:Array = loaderOrder(slider_obj.itemNum, startPanel);
var n:int;
for (var i:int = 0; i < slider_obj.itemNum; i++) {
n = loadorder[i];
img_loaders[n] = new Loader();
img_loaders[n].name = "loader_" + n;
img_loaders[n].contentLoaderInfo.addEventListener(Event.COMPLETE , imageComplete);
img_loaders[n].contentLoaderInfo.addEventListener(IOErrorEvent.I O_ERROR, throwIOError);

hover_loaders[n] = new Loader();
hover_loaders[n].name = "hoverloader_" + n;
hover_loaders[n].contentLoaderInfo.addEventListener(Event.COMPLETE , hoverComplete);
hover_loaders[n].contentLoaderInfo.addEventListener(IOErrorEvent.I O_ERROR, throwIOError);

switch(slider_obj.itemType[n].toString()) {
case "img":
itemPath = slider_obj.imagePath;
break;

case "swf":
itemPath = slider_obj.swfPath;
break;

default:
itemPath = slider_obj.imagePath;
break;
}


var imgToLoad:URLRequest = new URLRequest(itemPath + slider_obj.itemImage[n].toString());
img_loaders[n].load(imgToLoad);

var hoverImageURL:URLRequest = new URLRequest(itemPath + slider_obj.itemOver[n]);
hover_loaders[n].load(hoverImageURL);
}

preloader = new(preload);
addChild(preloader);

if (showPreloader == false) {
preloader.visible = false;
}

preloader.x = 485;
preloader.y = 300;


}

//================================================== ================================================== ==============
//--- IMAGE LOAD HANDLERS
//================================================== ================================================== ==============

function imageComplete(e:Event):void {
//Set looping to true once length has been determined
looping = false;

//removeChildAt(0);

count++;
var n:Number = Number(e.target.loader.name.substr(7));
img_arr[n] = e.target.content;
//bitMap = Bitmap(e.target.content);
//bitMap.smoothing = true;

addChild(img_arr[n]);
img_arr[n].visible = false;

if (clickable == true && slider_obj.itemType[n].toString() != "swf") enableClicks(img_arr[n], n);


if (n == startPanel) {
slideTo(startPanel);
}

}

function hoverComplete(e:Event) {
var n:Number = Number(e.target.loader.name.substr(12));

var bitMap:Bitmap = e.target.content;
if(bitMap != null)
bitMap.smoothing = true;

hover_arr[n] = bitMap;
addChild(bitMap);
bitMap.visible = false;

setChildIndex(bitMap, numChildren-1);
setChildIndex(leftBtn, numChildren-1);
setChildIndex(rightBtn, numChildren-1);
setChildIndex(over, numChildren-1);
}

//================================================== ================================================== ==============
//--- LOAD LEFT/RIGHT BUTTONS and OVER BUTTON
//================================================== ================================================== ==============
var leftBtn:MovieClip;
var rightBtn:MovieClip;
var over:MovieClip;

function loadButtons():void {
leftBtn = new (left_btn);
rightBtn = new (right_btn);
over = new (overbtn);

leftBtn.name = "leftBtn";
rightBtn.name = "rightBtn";
over.name = "over";

leftBtn.x = 10;
leftBtn.alpha = 0;
leftBtn.y = 300;
rightBtn.x = 940;
rightBtn.alpha = 0;
rightBtn.y = 300;

over.alpha = 0;

over.x = 960 - over.width;
over.y = 540 - over.height;
over.visible = true;

addChild(over);
addChild(leftBtn);
addChild(rightBtn);

Tweener.addTween(leftBtn, {alpha:100, time:1, transition:"easeInOutExpo", delay: .5});
Tweener.addTween(rightBtn, {alpha:100, time:1, transition:"easeInOutExpo", delay: .5});
Tweener.addTween(over, {alpha:100, time:1, transition:"easeInOutExpo", delay: .5});

if (showNav == true) {
enableButton(leftBtn);
enableButton(rightBtn);
enableButton(over);
} else {
leftBtn.visible = false;
rightBtn.visible = false;
over.visible = false;
}

checkButtons();

}

//================================================== ================================================== ==============
//--- BUTTON HANDLERS
//================================================== ================================================== ==============
function handleRollOver(e:MouseEvent):void {
switch(e.currentTarget.name) {
case "rightBtn":
e.target.gotoAndStop("over");
break;
case "leftBtn":
e.target.gotoAndStop("over");
break;
case "over":
e.target.gotoAndStop("over");
hover_arr[nowShowing].visible = true;
break;
}
}

function onComplete(e:Event) {
var bitMap2:Bitmap = e.target.content;
if(bitMap2 != null)
bitMap2.smoothing = true;
addChildAt(testload, numChildren - 3);
}


function handleRollOut(e:MouseEvent):void {
switch(e.currentTarget.name) {
case "rightBtn":
e.target.gotoAndStop("up");
break;
case "leftBtn":
e.target.gotoAndStop("up");
break;
case "over":
e.target.gotoAndStop("up");
hover_arr[nowShowing].visible = false;
break;
}
}


function handleRelease(e:MouseEvent):void {
if (timer) { timer.stop(); }
var n;

switch(e.currentTarget.name) {
case "rightBtn":
n = nextPicIndex(nowShowing);
if (n !== null) {
slideFWD(n);
}
break;

case "leftBtn":
n = prevPicIndex(nowShowing);
if (n !== null) {
slideRWD(n);
}
break;
}
}


//================================================== ================================================== ==============
//--- FUNCTIONS TO ENABLE/DISABLE BUTTONS
//================================================== ================================================== ==============
function disableButton(btn:Object):void {
btn.gotoAndStop("inactive");
btn.alpha = .2;
btn.removeEventListener(MouseEvent.MOUSE_OUT, handleRollOut);
btn.removeEventListener(MouseEvent.MOUSE_OVER, handleRollOver);
btn.removeEventListener(MouseEvent.CLICK, handleRelease);
btn.buttonMode = false;
}

function enableButton(btn:Object):void {
btn.gotoAndStop("up");
btn.alpha = 100;
btn.addEventListener(MouseEvent.MOUSE_OUT, handleRollOut);
btn.addEventListener(MouseEvent.MOUSE_OVER, handleRollOver);
btn.addEventListener(MouseEvent.CLICK, handleRelease);
btn.buttonMode = true;
}

function checkButtons():void {
if ( prevPicIndex(nowShowing) === null) {
disableButton(leftBtn);
} else {
enableButton(leftBtn);
}
if ( nextPicIndex(nowShowing) === null) {
disableButton(rightBtn);
} else {
enableButton(rightBtn);
}
enableButton(over);

// Make sure the buttons stay on top
try {
setChildIndex(hover_arr[nowShowing], numChildren-1);
} catch (e) {}
setChildIndex(leftBtn, numChildren-1);
setChildIndex(rightBtn, numChildren-1);
setChildIndex(over, numChildren-1);
}

function disableAllButtons():void {
disableButton(leftBtn);
disableButton(rightBtn);
disableButton(over);
setChildIndex(leftBtn, numChildren-1);
setChildIndex(rightBtn, numChildren-1);
setChildIndex(over, numChildren-1);
}