0

我有一个将根据时间显示的各种拍卖物品的 XML 文件。我想使用 AS3 根据 XML 文件字段中设置的小时来获取UTCDate,然后显示相应的 XML 节点。我对 AS3+XML 非常熟悉,所以我可以使用一些帮助。我已经完成了所有设置,但有些地方不太对劲。

AS3 代码:

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;
    }
}

}

我是否需要从 xml 声明不同的时间,例如每次都做一个 var 什么的?我怎样才能得到它,以便 AS3 知道时间节点是实际时间?这是一些示例 XML 代码。(我也没有调用图像,但我只是想让这个东西首先读取适当的节点。)它没有引发编译器错误,但它也不确定要做什么。

XML 代码:

<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>

任何帮助将不胜感激。

好的,根据下面的评论,我现在有了这个:

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;
}

无论我将时钟设置为什么时间,这都会返回最后一个节点。我可以说这很接近,但它似乎不限于节点时间,而只是循环到最后。

更新

好的,这是最新的代码 - 现在不产生数据但不引发编译器错误。时钟调整到什么都没关系:

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;
}
4

1 回答 1

0
var my_date:Date = new Date();
var currentHour:String = my_date.getUTCHours() +":" + my_date.getUTCMinutes();

— 您将获得 HH:MM 格式的 UTC 时间。

trace(xml.auction.(time == currentHour))

将跟踪节点,该节点具有“时间”标签值 == currentHour


但在你的情况下,我们正在比较时间。确保您解析 xml 时间值并将其转换为 Number 正确的方式。

您的fileLoaded函数应如下所示:

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; 
    }       
}

还要定义parseTime函数,如果您的 XML 时间是 UTC

function parseTime(time:String):Number {
    var hour:int = time.split(":")[0];
    var minute:int = time.split(":")[1];
    return hour + minute / 60;
}

或者如果您的XML 时间是您的本地时间

function parseTime(time:String):Number {
    var hour:int = time.split(":")[0];
    var minute:int = time.split(":")[1];
    var date:Date = new Date();
    date.setHours(hour);
    date.setMinutes(minute);
    return date.getUTCHours() + date.getUTCMinutes() / 60
}
于 2013-07-08T19:22:16.337 回答