在单个列表中显示sharedObject.data属性的multipe数组类型属性

时间:2013-04-22 21:41:56

标签: actionscript-3 flash-cs6

我有各种食物类别页面(例如" Carbohyrates"," Meat"," Vegetables"等),每个页面上有3个组合框用户可以从每个类别中选择3种不同的成分(即在" Meat"页面上,用户可以选择3种不同类型的肉)。我将这三种肉类作为数组写入一个共享对象:

这是我的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个不同的功能中)。除了复选框和组合框不同之外,它们都非常相似。

我有一个名为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())的某个属性的so.data.meatItems表示输入TextField,但我想输入无论他们拥有什么子属性,我的sharedObject中的所有实例。另请注意,当我想要评估length <中的所有项目时,我正在meatItems循环条件中评估for数组的sharedObject / p>

我该怎么做?

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

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来评估每个共享对象属性的内容是否为空 - 因为我相信如果数组为空,这可能会导致另一个错误?)

1 个答案:

答案 0 :(得分: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