2013-04-22 66 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