0

我有各种食品类别页面(例如“碳水化合物”、“肉类”、“蔬菜”等),每个页面上有 3 个组合框,用户可以从每个类别中选择 3 种不同的成分(即在“肉类”页面上,一个用户可以选择 3 种不同类型的肉)。我将这 3 个肉类作为数组写入 sharedObject,如下所示:

这是我的saveMeat()函数作为示例,因此您可以了解我是如何形成数组的:

function saveMeat(event:MouseEvent):void
{
    _categoryMeat.btn_goback.removeEventListener(MouseEvent.CLICK, saveMeat);
    removeChild(_categoryMeat);

    if (meatDisableCheckBox.selected == true)
    {
        stop();
    } 

    if (myComboBoxMeat.selectedLabel != null)
    {
        so.data.meat1 = myComboBoxMeat.selectedLabel;
        trace(so.data.meat1);
    }
    if (myComboBoxMeat2.selectedLabel != null)
    {
        so.data.meat2 = myComboBoxMeat2.selectedLabel;
        trace(so.data.meat2);
    }
    if (myComboBoxMeat3.selectedLabel != null)
    {
        so.data.meat3 = myComboBoxMeat3.selectedLabel;
        trace(so.data.meat3);
    } 
    var meat_items_array:Array = new Array(so.data.meat1, so.data.meat2, so.data.meat3);
    so.data.meatItems = meat_items_array;
    so.flush();
    trace(so.data.meatItems);
}

每个类别页面都有几个这样的功能(在所有 6 个不同的功能中)。除了复选框和组合框不同之外,它们都非常相似。

我有一个名为 list 的函数dataLoaded,它将 sharedObject 中的项目加载到滚动列表中:

private function dataLoaded():void
{
    var i:Number;
    for (i=0;  i < so.data.meatItems.length; i++) {
        _item = new Item();
        // creates the var itemTextField //
        _itemTextField = new TextField();
        _itemTextField.text += '' + so.data.meatItems[i].toString();
        //adds textfield to displaylist//
        _item.addChild(_itemTextField);
    }
}

如您所见,for循环将我的 sharedObject ( ) 的toString()属性之一的表示输入到 中,但我想输入我的 sharedObject 内的所有实例,无论它们具有什么子属性。另请注意,当我想评估循环中的所有项目时,我正在评估循环条件中的数组so.data.meatItemsTextFieldlengthmeatItemsforsharedObject

我怎样才能做到这一点?

编辑:我实施了以下解决方案,但收到此错误:

TypeError: Error #1010: A term is undefined and has no properties.
    at RecipeMatcher/dataLoaded()[/Users/adambull/Desktop/RecipeMatcherSO/RecipeMatcher.as:893]
    at RecipeMatcher/displayList()[/Users/adambull/Desktop/RecipeMatcherSO/RecipeMatcher.as:212]
    at RecipeMatcher/hideSplashScreen()[/Users/adambull/Desktop/RecipeMatcherSO/RecipeMatcher.as:192]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at SetIntervalTimer/onTimer()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()

这是我实现以下内容的尝试(这次我已经包含了我的全部功能,以防万一我的功能中有其他东西导致问题)

private function dataLoaded():void
{
    // parsing of each ingredient//
    // instantiation of mcItem (the stage for each item)
    for (var item:* in so.data)
    {
        if (so.data[item] !=null)
        {
            if (so.data[item] is Array)
            {
                var a:Array = so.data[item];
                for (var i:uint = 0 ; i < a.length;i++ )
                {
                    _item = new Item();
                    // sets //over// layer to invisible / transparent //
                    _item.item_btn_over.alpha = 0;
                    // creates the var itemTextField //
                    _itemTextField = new TextField();
                    // _itemTextField visual attributes //
                    _itemTextField.x = _textFieldXPosition + _textFieldPaddingLeft;
                    _itemTextField.y = _textFieldYPosition;
                    _itemTextField.selectable = true;
                    _itemTextField.wordWrap = true;
                    itemTextField.width = _textFieldWidth;
                    _itemTextField.height = _textFieldHeight;
                    _itemTextField.embedFonts = true;
                    _defaultFormat.color = 0x111112;
                    _defaultFormat.font = _arialRounded.fontName;
                    _defaultFormat.size = 18;
                    _itemTextField.defaultTextFormat = _defaultFormat;
                    _itemTextField.appendText( so.data[item][i].toString() );
                    //adds textfield to displaylist//
                    _item.addChild(_itemTextField);
                    //vertical positioning//
                    _item.y = i * _itemPosition;
                    _item.btn_delete.visible = false;
                    _item.buttonMode = true;
                    _item.mouseChildren = false;
                    //adds items to container displaylist//
                    _container.addChild(_item);
                }
            }
        }
    }
    // Input Mask//
    _mask = new Shape();
    _mask.graphics.beginFill(0xFF0000);
    _mask.graphics.drawRect(0, 0, _maskWidth, _maskHeight);
    _mask.graphics.endFill();
    // Positioning of input mask//
    // horizontal centering of input mask//
    _mask.x = stage.stageWidth / 2 - _container.width / 2;
    _mask.y = _paddingTop;
    // adds the mask onto the stage//
    addChild(_mask);
    // assigns the above mask to the container //
    _container.mask = _mask;
    // Positioning of container with the mask//
    // horizontal centering of container //
    _container.x = stage.stageWidth / 2 - _container.width / 2;
    // vertical position of container //
    _container.y = _paddingTop;

    //Container background stylings//
    _background = new Shape();

    _background.graphics.drawRect(0, 0, _container.width, _container.height);

    _container.addChildAt(_background, 0);
    //End of container background stylings//
    _item.parent.addEventListener( MouseEvent.CLICK, itemClicked );
    _container.addEventListener(MouseEvent.MOUSE_OVER, movingOver);
    _container.addEventListener(MouseEvent.MOUSE_OUT, movingOut);
}

(我尝试添加额外if的内容来评估每个共享对象属性的内容是否为空 - 因为我相信如果数组为空,这可能会导致另一个错误?)

4

1 回答 1

1

如果我理解你的问题,这里有一个例子。它浏览 so.data,查找数组,然后对每个数组进行迭代。

import flash.net.SharedObject;

var my_so = SharedObject.getLocal("superfoo");
// fill some fake values
var ar:Array = [1, 2, 3, 4, 5]
var ar2:Array = ['a1', 'a2', 'a3', 'a4']
my_so.data.array1 = ar;
my_so.data.array2 = ar2;
my_so.data.notarray = 'I m not an array';
my_so.flush();


// browse the so and find arrays
var my_so2 = SharedObject.getLocal("superfoo");
for (var item:* in my_so2.data) {       
    if (my_so2.data[item] is Array) {
        var a:Array = my_so2.data[item];
        for(var i:uint = 0 ; i<a.length;i++ ) {
            trace('my_so2.data[' + item + '][' + i + ']=' + a[i])
        }
    }
}

输出(它会跳过 so.data 中不是数组的项目)

my_so2.data[array2][0]=a1
my_so2.data[array2][1]=a2
my_so2.data[array2][2]=a3
my_so2.data[array2][3]=a4
my_so2.data[array1][0]=1
my_so2.data[array1][1]=2
my_so2.data[array1][2]=3
my_so2.data[array1][3]=4
my_so2.data[array1][4]=5
于 2013-04-22T21:56:38.497 回答