2013-04-22 65 views
0

我有各个食品分类页面(例如“Carbohyrates”,“Meat”,“Vegetables”等),每个页面上有3个组合框可以从每个类别中选择3种不同的配料(即在“肉类”页面上,用户可以选择3种不同类型的肉类)。我写这3对肉类的共享对象作为这样一个数组:在单个列表中显示sharedpeeddata属性的multipe数组类型属性

这里是我的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列表功能加载从共享对象的项目进入滚动列表:

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回路输入我的我的共享对象的属性之一的toString()表示( so.data.meatItems)转换为TextField,但我想输入我的sharedObject中的所有实例,而不管它们具有哪些子属性。还要注意,我在评估meatItems阵列的lengthfor循环条件,如果我想成为在sharedObject

我该怎么做评估的所有项目?

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

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

如果我明白你的问题,这里是一个例子。它浏览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 
+0

是的,你没有正确地理解我的问题。不幸的是,当我实现你的代码时,它给了我“错误#1010:一个术语是未定义的,没有属性。”在线891上是'_itemTextField.appendText ...'行? – adaam 2013-04-22 22:01:51

+0

我编辑了我的示例,可以使其适应您的上下文。 – RafH 2013-04-22 22:18:55

+0

仍然收到相同的错误,不正确地更新我的问题与堆栈跟踪+我的解决方案的最新实施 – adaam 2013-04-22 22:35:32