2010-08-29 98 views
1

我試圖在數組內添加一個MovieClip實例。 House類內部是一個名爲HouseObjects的屬性。在這個數組中,我創建了一個Comp和一個Light類。動畫片段通過鏈接動態放置在舞臺上。影片剪輯也可以充當「切換按鈕」。如果按鈕狀態爲ON,則值爲1.如果按鈕狀態爲OFF,則值爲0.將動畫片段的動態實例添加到數組

如果值爲1,我試圖在onList Array中添加MovieClip實例。在該數組內部將有所有按鈕狀態爲ON的實例。

我創建了一個名爲objSelect的屬性。

var objSelect:Object; 

該變量包含currentTarget選定。我試圖將它傳遞給function trackItems,以便根據按鈕狀態在onList數組中按下/彈出它。

我收到有關此錯誤的信息: onList.pop(objSelect); 參數的數量不正確。預計不超過0.1

 public class House extends MovieClip 

    { 

     var HouseObjects:Array = new Array(); 
    var onList:Array = []; // instances added to this array that have a bstatus ON 
     var power:int; // holds value of individual House Objects 
     var bstate:int; // 0 or 1 (ON or OFF) 
     var bstatus:int; 
     var userInput:int; // stores user data (of selected data); 
     //holds value of e.currentTarget.power 

     var currentPower:int; // stores current power 
      var objSelect:Object; 

     public function House() 
     { 
     // Instances are MovieClip "toggle buttons" 
     HouseObjects[0] = new Comp(); // creates instance of Comp 
     HouseObjects[1] = new Light(); // creates instance of Light 
     } 

     function toggleClick(e:MouseEvent) { 
      // go to appropriate frame 
     if (e.currentTarget.currentFrame == 2) 
     { 
     e.currentTarget.gotoAndStop(3); 
     e.currentTarget.bstate = 1; 
     } 

     if (e.currentTarget.currentFrame == 4) 
     { 
     e.currentTarget.gotoAndStop(1); 
     e.currentTarget.bstate = 0; 
     } 

      bstatus = e.currentTarget.bstate; 
      objName = e.currentTarget.name; 

     trackItems(objSelect, bstatus); 


    } // end of function toggle click 



    function trackItems(objSelect:Object, bstatus:int):void 
    { 
    if (bstatus == 0) { 
     // remove objSelect from Array onList 

    } else if (bstatus == 1) { 
     onList.push(objSelect); 
     //add to Array onList  
    } 

} 

// function called when user clicks on update button 
function updateStage():void 
{ 
    for (var i:int = 0; i<=onList.length;i++) { 
    addChild(onList[i]); 
    } 

} 

}

+0

我想彈出只是從列表的末尾刪除項目。你的意思是推? – akonsu 2010-08-29 20:13:47

+0

@akonsu取決於對象的狀態(開/關) - 我需要將它推入或彈出陣列。如果對象的狀態爲ON - POP(添加到數組),如果OFF - PUSH(移出數組)。 – jc70 2010-08-29 20:18:07

回答

1

你並不需要環通陣列,以便找到要刪除的元素,簡單地使用indexOf方法:

 
var index:int = onList.indexOf(objSelect); 
onList.splice(index , 1); 

我建議只加一個對象的名稱到onList陣列,它使錯誤

 
//if the button status is On 
onList.push(objSelect.name); 

//if the button status is Off 
var index:int = onList.indexOf(objSelect.name); 
onList.splice(index , 1); 

比較更直接和更不容易,那麼你可以更新這樣的階段:

function updateStage():void 
{ 
    for (var i:int = 0; i<=HouseObjects.length;i++) 
    { 
    //if the onList Array contains the current name 
    if(onList.indexOf(HouseObjects[i].name) != -1) 
      addChild(HouseObjects[i]); 
    } 

} 
1

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Array.html#pop%28%29

流行從陣列中刪除最後一個元素,如果你想刪除特定的元素,使用方法Array.splice與deleteCount == 0,

+0

@alxx,我嘗試傳遞objSelect裏面的彈出(); - 但接收錯誤,說錯誤的參數數量,預計爲0。 – jc70 2010-08-29 20:21:25

+0

@ crewof1:對,這就是alxx所說的:你不能那樣做。 – akonsu 2010-08-29 20:23:06

+0

@alxx和@akonsu:是否可以將一個動畫片段的實例添加到數組中? – jc70 2010-08-29 20:25:58

0

pop刪除數組的最後一項。如果要刪除給定的項目,您需要將陣列向上的尾巴在要刪除

0

我的建議元素的位置,移動至使用更先進的集合,如
http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html#methodSummary

這裏是修改後的代碼:

public class House extends MovieClip 

{ 

    var HouseObjects:Array = new Array(); 
var onList:ArrayCollection = new ArrayCollection(); // instances added to this array that have a bstatus ON 
    var power:int; // holds value of individual House Objects 
    var bstate:int; // 0 or 1 (ON or OFF) 
    var bstatus:int; 
    var userInput:int; // stores user data (of selected data); 
    //holds value of e.currentTarget.power 

    var currentPower:int; // stores current power 

    public function House() 
    { 
    // Instances are MovieClip "toggle buttons" 
    HouseObjects[0] = new Comp(); // creates instance of Comp 
    HouseObjects[1] = new Light(); // creates instance of Light 
    } 

    function toggleClick(e:MouseEvent) { 
     // go to appropriate frame 
    if (e.currentTarget.currentFrame == 2) 
    { 
    e.currentTarget.gotoAndStop(3); 
    e.currentTarget.bstate = 1; 
    } 

    if (e.currentTarget.currentFrame == 4) 
    { 
    e.currentTarget.gotoAndStop(1); 
    e.currentTarget.bstate = 0; 
    } 

     bstatus = e.currentTarget.bstate; 
     objName = e.currentTarget.name; 

    trackItems(objSelect, bstatus); 


    } // end of function toggle click 



    function trackItems(objName:Object, bstatus:int):void 
    { 
    if (bstatus == 0) { 
     onList.removeItemAt(onList.getItemIndex(objName)); 
     // remove from onList 

    } else if (bstatus == 1) { 
     onList.addItem(objName); 
     //add to onList  
    } 

} 

// function called when user clicks on update button 
function updateStage():void 
{ 
    for (var i:int = 0; i<=onList.length;i++) { 
    addChild(onListgetItemAt(i)); 
    } 

} 
+0

感謝您的回覆。我嘗試使用var onList:ArrayCollection = new ArrayCollection();但注意到ArrayCollection,removeItemAt()和addItem在Flash中未被識別。我搜索了一下,並在論壇上看到Flex中使用了ArrayCollection,並且可以在AS3中創建一個ArrayCollection。我需要更多地瞭解如何做到這一點,但感謝您的意見。 – jc70 2010-08-29 23:20:47

1

創建的認定,需要被移除,並在objSelect通過了該項目的功能。找到項目後,使用splice()。

function trackItems(objSelect:Object, bstatus:int):void 
{ 
    if (bstatus == 0) { 
     //remove instance from onList array  
     // call function removeArrayItem 
     removeArrayItem(objSelect); 

    } else if (bstatus == 1) { 
     //remove instance from onList array  
     onList.push(objSelect); 
    } 
} 

function removeArrayItem(objSelect:Object):void 
{ 
     var arrayLength:int = onList.length; 

     // Loop through array to find item that needs to be removed 
     for (var i:int=0; i<arrayLength; i++) 
     { 
      if (onList[i] == objSelect) 
      { 
       onList.splice(i, 1); 
      } 
     } 

} 
+0

您不需要通過數組循環,只需使用indexOf方法即可:var index:int = onList.indexOf(objSelect); onList.splice(index,1); – PatrickS 2010-08-30 02:03:03