我有一个对象数组,每个对象在首次创建时都会分配一个ID。我赋予用户视觉上重新排序对象的能力,这些对象会改变它们在数组中的位置。然后,他们可以选择使用flash sharedObject或“cookie”保存该订单,然后,如果他们重新打开flash文件,我希望他们能够点击按钮来恢复该订单。我只是不确定在数组中设置对象索引的语法是什么。这是我的代码:
变量:
var project_settings = SharedObject.getLocal("settings"); //saves all project settings for the next time the file is opened
var project_order:Array = []; //saves project order for the next time the file is opened
var project_display:Array = []; //saves whether each project should be displayed or hidden for the next time the file is opened
保存代码:
function saveOrder(){
for (var i=0;i<project_array.length;i++){
project_order[i] = project_array[i].id;
project_display[i] = project_array[i].projectThumb.thumbActive;
}
project_settings.data.order = project_order;
project_settings.data.active = project_display;
//trace (project_settings.data.active[1]);
project_settings.flush(); //saves most recent "cookie"
}
恢复代码:
function loadOrder(){
for (var i=0;i<project_array.length;i++){
/* NEED THE CODE THAT GOES HERE. BASICALLY, PROJECT_ARRAY[i] SHOULD BE THE ITEM WITH AN ID EQUAL TO PROJECT_SETTINGS.DATA.ORDER[i] */
}
}
答案 0 :(得分:1)
这样的事情应该有效:
function loadOrder()
{
var dict = new Dictionary();
for (var i = 0; i < project_array.length; i++)
dict[project_array[i].id] = project_array[i];
project_array = [];
for (var i = 0; i < project_settings.data.order.length; i++)
project_array[i] = dict[project_settings.data.order[i]];
}
答案 1 :(得分:0)
只需加载数组并对ID进行排序即可。这样的事情应该有效:
private function _loadArray():void
{
// fill in your array
project_array.sort( this._sortFunc );
}
// replace the * by whatever your object type is
private function _sortFunc( a:*, b:* ):int
{
return a.id - b.id;
}
更多信息:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#sort()
甚至sortOn()
函数(可能更容易)也应该有效:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#sortOn()