在 flash as3 中重新排序数组

发布于 2024-10-26 02:25:19 字数 1254 浏览 1 评论 0原文

我有一个对象数组,每个对象在首次创建时都分配有一个 ID。我让用户能够直观地重新排序对象,从而改变它们在数组中的位置。然后,他们可以选择使用 flash 共享对象或“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] */
     }


}

I have an array of objects, each of which is assigned an ID when it is first created. I give the user the ability to visually reorder the objects, which changes their position in the array. They then have the option to save that order using a flash sharedObject or "cookie" and then later, if they reopen the flash file, I want them to be able to hit a button to restore that order. I'm just not sure what the syntax would be to set the object's index within the array. Here's my code:

VARIABLES:

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

SAVE CODE:

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

RESTORE CODE:

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] */
     }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

善良天后 2024-11-02 02:25:19

像这样的东西应该有效:

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

Something like this should work:

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]];
}
仙女 2024-11-02 02:25:19

只需加载数组并按 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()

Just load in your array and sort on the ID. Something like this should work:

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

More info: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#sort()

Or even the sortOn() function (which might be easier) should work:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#sortOn()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文