2010-08-29 59 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

相关问题