LOOPING THROUGH XML DYNAMICALLY

Posted by ahsi 
LOOPING THROUGH XML DYNAMICALLY
August 27, 2009 08:51AM
Ive got this looping through my xml nodes which works fine, but the transition effect is only happening at "hide". Im guessing this is a loading issue? ill get back to it.

import com.jumpeye.Events.FLASHEFFEvents;

// The Timer object is used to put a delay of 1 second between the
// show and hide transitions.
var timerObject:Timer = new Timer(5000);

// The transition type which will change from "show" to "hide" and
// "hide" to "show" after each transition has endded.
var transitionType:String = "show";

// The index of the current line from the xml file (the <text> nodes).
var textIndex:int = 0;

// The XML object that will hold the data read from the xml file.
var xmlData:XML;

var loaderObject:URLLoader = new URLLoader();
loaderObject.load(new URLRequest("gallery.xml"));

loaderObject.addEventListener(Event.COMPLETE, loaderHandler);
timerObject.addEventListener(TimerEvent.TIMER, timerHandler);
fe.addEventListener(FLASHEFFEvents.TRANSITION_END, transitionHandler);

// After the xml file has loaded, the first line of text is set into
// the text field and then the TxEff component instance is set up and
// starts the "show" transition.
function loaderHandler(eventObj:Event):void {
    xmlData = XML(loaderObject.data);
	ldr.source = xmlData.img[textIndex]
	fe._targetInstanceName = "ldr";
	fe.transitionEffect(transitionType);
}

// This timer handler is called whenever the timer object has reached
// the delay of 1 second. Then the timer is stopped and a new show or
// hide transition starts.
function timerHandler(eventObj:TimerEvent):void {
	timerObject.stop();
	if (transitionType == "show") transitionType = "hide";
	else transitionType = "show";
	fe.transitionEffect(transitionType);
}

// This functin is called whenever a show or hide transition ends.
function transitionHandler(evtObj:FLASHEFFEvents):void {
	if (transitionType == "show") {
		// When a show transition has endded, the timer starts, so it
		// will introduce a delay of one second until the hide
		// transition starts.
		timerObject.start();
	}
	else {
		// When a hide transition has endded, the text from the next
		// <text> node is written into the text field. Then, the
		// transition type is set to "show" and a new show transition
		// starts. When the last line of text has been displayed, the
		// index is set back to 0, so the text will be displayed all
		// over again.
		textIndex++;
		if (textIndex == xmlData.img.length()) textIndex = 0;
		ldr.source = xmlData.img[textIndex]
		transitionType = "show";
		fe.transitionEffect(transitionType);
	}
}
Re: LOOPING THROUGH XML DYNAMICALLY
August 27, 2009 12:55PM
1. You don't need a timer to set the delay between hide and show. FlashEff has a delay setting that you can change to do that exact thing.
2. Make sure that you are starting the show effect only after the picture has finished loading. You will need to listen to the load.complete event on the loader and only then start the show transition.
Hope this helps,

-Ionut
Re: LOOPING THROUGH XML DYNAMICALLY
August 27, 2009 05:25PM
thank you lonut for your response. i see there is a delay, but i didnt know you can cycle through xml like a slideshow. that would be alot easier.
Re: LOOPING THROUGH XML DYNAMICALLY
August 27, 2009 07:47PM
I got it working. just added some complete event listeners. i aint a coder, so if there is an easier way please let me know, otherwise this is good enough for me.

import com.jumpeye.Events.FLASHEFFEvents;

var timerObject:Timer = new Timer(5000);

var transitionType:String = "show";

var textIndex:int = 0;

var xmlData:XML;

var loaderObject:URLLoader = new URLLoader();
loaderObject.load(new URLRequest("gallery.xml"));

loaderObject.addEventListener(Event.COMPLETE, loaderHandler);
timerObject.addEventListener(TimerEvent.TIMER, timerHandler);
fe.addEventListener(FLASHEFFEvents.TRANSITION_END, transitionHandler);

function loaderHandler(eventObj:Event):void {
    xmlData = XML(loaderObject.data);
	ldr.source = xmlData.img[textIndex]
	fe._targetInstanceName = "ldr";
	ldr.addEventListener(Event.COMPLETE, completeHandler2);
	
}

function completeHandler2(event:Event):void {
fe.transitionEffect(transitionType);
ldr.removeEventListener(Event.COMPLETE, completeHandler2);
}

function timerHandler(eventObj:TimerEvent):void {
	timerObject.stop();
	if (transitionType == "show") transitionType = "hide";
	else transitionType = "show";
	fe.transitionEffect(transitionType);
}

function transitionHandler(evtObj:FLASHEFFEvents):void {
	if (transitionType == "show") {
		timerObject.start();
	}
	else {
		textIndex++;
		if (textIndex == xmlData.img.length()) textIndex = 0;
		ldr.source = xmlData.img[textIndex]
		ldr.addEventListener(Event.COMPLETE, completeHandler);
		
	}
	
function completeHandler(event:Event):void {
transitionType = "show";
fe.transitionEffect(transitionType);
ldr.removeEventListener(Event.COMPLETE, completeHandler);
}
}



Edited 1 time(s). Last edit at 08/27/2009 07:49PM by ahsi.
Re: LOOPING THROUGH XML DYNAMICALLY
August 28, 2009 07:01AM
Hi,

It looks nice and clean, you say your not a coder but you could have fooled me. Nice work.
Thank you for the tutorial.

-Ionut.
Re: LOOPING THROUGH XML DYNAMICALLY
September 30, 2009 07:18AM
Hi,

I'm also trying this. Do you have an example of how you set up your XML file please?

Thanks,
Sorry, you do not have permission to post/reply in this forum.