我从xml中获取数据并将其存储在数组中。我编码如下
Private var dataarray=new Array();
private var timearray=new Array();
private var distancearray=new Array();
private var detailStock : String;
private var distancedata : String;
private var timedata : String;
private var datalist : String;
private var data : String;
function readMe() {
var filepath : String = Application.dataPath+"/XmlDocs/"+"Stock"+".xml";
var xmlDoc : XmlDocument = new XmlDocument();
if(File.Exists (filepath))
{
xmlDoc.Load( filepath );
var Stock_list : XmlNodeList = xmlDoc.GetElementsByTagName("Stock");
for(var i : int = 0; i < Stock_list.Count;i++)
{
// getting child nodes of stock, like Rice and Wheat in a list
var StockItems_list : XmlNodeList = Stock_list.Item(i).ChildNodes;
// running a loop through all items present in the stock
for(var j : int = 0; j < StockItems_list.Count; j++)
{
//Debug.Log("StockItems_list count"+StockItems_list.Count);
// taking the properties of a item into a list like Price and Quantity
var StockItemsProperties_list : XmlNodeList = StockItems_list.Item(j).ChildNodes;
for(var k : int = 0; k < StockItemsProperties_list.Count; k++)
{
//Debug.Log("length....."+ StockItemsProperties_list[k].InnerText);
data=StockItemsProperties_list[k].InnerText+"\t";
//Debug.Log(data);
/*--------Pushed the full data into an array-----------*/
// dataarray=new Array();
dataarray.Push(data);
//Debug.Log("Elements in the array"+dataarray);
}
//var StockItemsProperties_list : XmlNodeList =StockItems_list[j].
// Getting Names of Items like Rice and Wheat
detailStock+="\n"+StockItems_list[j].Name+"\n";
// We know that Price is stored at Oth element of StockItemsProperties_list
detailStock+=StockItemsProperties_list[0].Name+" ";
// Inner Text of StockItemsProperties_list[0] (Price node) Contains the money
detailStock+=StockItemsProperties_list[0].InnerText+" "+"Rs"+"\n";
// And Quantity at 1st element of StockItemsProperties_list
detailStock+=StockItemsProperties_list[1].Name+" ";
// Inner Text of StockItemsProperties_list[1] (Quantity node) Contains the Quantity in Kg
detailStock+=StockItemsProperties_list[1].InnerText+" "+"Kg"+"\n";
//Debug.Log("distance"+StockItemsProperties_list[0].InnerText);
//Debug.Log("seconds"+StockItemsProperties_list[1].InnerText);
distancedata=StockItemsProperties_list[0].InnerText;
distancearray=new Array();
distancearray.Push(distancedata);
//Debug.Log("distance Array............ "+distancearray);
/*--------------Print the time into time array -----------------*/
timedata=StockItemsProperties_list[1].InnerText;
timearray=new Array();
timearray.Push(timedata);
// Debug.Log("time Array............ "+timearray);
/*-------------Distance and time from both array array ------------------*/
for(var d=0;d<distancearray.length;d++)
{
for(var t=0;t<timearray.length;t++)
{
// Debug.Log("distance : " +distancearray[d] +" "+"time: "+timearray[t]+"\n");
transform.position-=new Vector3(0,distancearray[d],0);
Debug.Log(transform.position);
}
}
使用上面的代码,我可以打印值。
但我想根据存储在distancearray中的值移动我的对象。 transform.position- = new Vector3(0,distancearray [d],0);
移动对象的代码获得以下异常
InvalidCastException: Cannot cast from source type to destination type.
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible)
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value)
Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value)
xmlDataReader.ReadXml () (at Assets/script/xmlDataReader.js:286)
xmlDataReader.OnGUI () (at Assets/script/xmlDataReader.js:141)
问题是什么。变量声明类型有什么问题吗?。请帮帮我
答案 0 :(得分:0)
这是UnityScript。
问题可能是由distancedata为String引起的,因此distancearray是String的数组,而不是int。
试试这个:
private var distancedata : int;
...
distancedata = parseInt(StockItemsProperties_list[0].InnerText);
&#13;
虽然格式错误的XML也可能是原因。