I have an XML file of various auction items that will display based on the time. I want to use AS3 to getUTCDate based on the hour set in the field of the XML file and then display the appropriate XML node. I'm pretty much intermediate with AS3+XML so I could use some help. I've got everything setup but something isn't quite right.
AS3 Code:
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.text.TextFormat;
var xmlData:XML = new XML();
var theURL_ur:URLRequest = new URLRequest("sample_auction.xml");
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);
var my_date:Date = new Date();
var currentHour:Number = my_date.getUTCHours() + (my_date.getUTCMinutes() / 60);
trace(currentHour);
function fileLoaded(e:Event):void
{
xmlData = XML(loader_ul.data);
for(var i:uint = 0; i < xmlData.auction.length(); i++)
{
if(xmlData.auction[i].time < currentHour)
{
auctionName_txt.text = xmlData.auction[i].title;
auctionDesc_txt.text = xmlData.auction[i].description;
}
}
}
do I need to declare the various times from xml such as doing a var on each time or something? how can I get it so the AS3 knows that the time node is an actual time? Here is some sample XML code. (also I'm not calling the image yet I'm just trying to get this thing to read the appropriate nodes first.) It's not throwing a complier error but it's also not sure what to do yet.
The XML Code:
<auctionlist>
<auction>
<time>6:00</time>
<title>Example Auction 1</title>
<description>Placeholder Text blah blah.</description>
<image>sampleimage1.jpg</image>
</auction>
<auction>
<time>7:00</time>
<title>Example Auction 2</title>
<description>Placeholder text blbalblbladflknsdf</description>
<image>imagepath11.jpg</image>
</auction>
<auction>
<time>8:00</time>
<title>Sample Auction 3</title>
<description>Placeholder text asflkamdflkmasdfm</description>
<image>imagepath12.jpg</image>
</auction>
<auction>
<time>9:00</time>
<title>Sample Auction 4</title>
<description>Placeholder text blabjadsflkm afdlkmasf afmlksf mmasdflkm</description>
<image>imagepath12.jpg</image>
</auction>
any help would be greatly appreciated.
Ok based on the comments below, I now have this:
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.text.TextFormat;
flash.system.Security.allowDomain('*');
var xmlData:XML = new XML();
var theURL_ur:URLRequest = new URLRequest("sample_auction.xml");
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);
var my_date:Date = new Date();
var currentHour:Number = my_date.getUTCHours() + (my_date.getUTCMinutes() / 60);
trace(currentHour);
function fileLoaded(e:Event):void
{
xmlData = XML(loader_ul.data);
for(var i:uint = 0; i < xmlData.auction.length(); i++)
{
var started:XMLList = xmlData.auction.(parseTime(time) >= currentHour);
for each (var item:XML in started)
{
auctionName_txt.text = xmlData.auction[i].title;
auctionDesc_txt.text = xmlData.auction[i].description;
}
}
}
function parseTime(time:String):Number {
var hour:int = time.split(":")[0];
var minute:int = time.split(":")[1];
return hour + minute / 60;
}
This returns the last node no matter what time I set my clock to. I can tell this is close but it seems to not restrict to the node time but just loop through to the end.
UPDATED
Ok here is latest code - now produces no data but throws no compiler errors. Doesn't matter what the clock is adjusted to:
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.text.TextFormat;
flash.system.Security.allowDomain('*');
var xmlData:XML = new XML();
var theURL_ur:URLRequest = new URLRequest("sample_auction.xml");
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);
var my_date:Date = new Date();
var currentHour:String = my_date.getUTCHours() +":" + my_date.getUTCMinutes();
trace(currentHour);
function fileLoaded(e:Event):void
{
xmlData = new XML(loader_ul.data);
var started:XMLList = xmlData.auction.(parseTime(time) < currentHour);
for each (var item:XML in started)
{
auctionName_txt.text = item.title;
auctionDesc_txt.text = item.description;
}
}
function parseTime(time:String):Number {
var hour:int = time.split(":")[0];
var minute:int = time.split(":")[1];
return hour + minute / 60;
}
— You will get UTC time in HH:MM format.
Will trace node, which has "time" tag value == currentHour
But in your case we are comparing time. Make sure you parse xml time value and cast it to Number right way.
Your fileLoaded function should look like this:
Also define parseTime function, if your XML time is UTC:
or if your XML time is your local time: