【问题标题】:Add dynamic instances of MovieClips to an array将 MovieClips 的动态实例添加到数组
【发布时间】:2010-08-29 20:11:30
【问题描述】:

我正在尝试在数组中添加一个 MovieClip 的实例。 House 类内部是一个名为 HouseObjects 的属性。在该数组中,我创建了一个 Comp 和一个 Light 类。影片剪辑通过链接动态放置在舞台上。影片剪辑也充当“切换按钮”。如果按钮状态为ON,则值为1。如果按钮状态为OFF,则值为0。

如果值为 1,我试图在 onList 数组中添加 MovieClip 实例。该数组中将包含按钮状态为 ON 的所有实例。

我创建了一个名为 objSelect 的属性。

var objSelect:Object;

该变量保存选定的 currentTarget。我正在尝试将它传递给function trackItems,以便根据按钮状态在 onList 数组中推送/弹出它。

我收到此行的错误: onList.pop(objSelect); 参数数量不正确。预计不超过 0。

     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]);
    }

 }

}

【问题讨论】:

  • 我猜 pop 只是从列表末尾删除了该项目。你的意思是推?
  • @akonsu 取决于对象的状态(开/关)——我需要将其从阵列中推出或弹出。如果对象的状态是 ON--POP(添加到数组),如果是 OFF--PUSH(从数组中删除)。

标签: flash arrays actionscript-3 dynamic instance


【解决方案1】:

你不需要遍历一个数组来找到要删除的元素,只需使用 indexOf 方法:

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

我建议只将对象的名称添加到 onList 数组中,这样比较更直接,更不容易出错

//如果按钮状态为On onList.push(objSelect.name); //如果按钮状态为关闭 var index:int = onList.indexOf(objSelect.name); onList.splice(索引, 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]);
  }

}

【讨论】:

    【解决方案2】:

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

    Pop 从数组中删除最后一个元素,如果要删除特定元素,请使用 Array.splice 和 deleteCount == 0。

    【讨论】:

    • @alxx,我尝试在 pop() 中传递 objSelect; --但是收到一个错误,说参数数量不正确,预期为 0。
    • @crewof1:对,这就是 alxx 的意思:你不能那样做。
    • @alxx 和@akonsu:是否可以将影片剪辑的实例添加到数组中?
    • @akonsu 我理解推送和弹出之间的区别。我遇到的问题是能够将变量 objSelect 传递给 push()。
    • 您不能将参数传递给 pop。它不带任何参数。它删除最后一个元素。它不需要参数。但是 push 另一方面,确实需要一个参数。你想要推动的东西。请提出您的问题。
    【解决方案3】:

    创建了一个函数,用于查找需要移除的项目,并在 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);
                }
            }
    
    }
    

    【讨论】:

    • 你不需要遍历 Array ,只需使用 indexOf 方法: var index:int = onList.indexOf(objSelect); onList.splice( 索引 , 1 );
    【解决方案4】:

    pop 删除数组的最后一项。如果要删除给定的项目,则需要将数组的尾部向上移动到要删除的元素的位置

    【讨论】:

      【解决方案5】:

      我的建议是使用更高级的集合,例如
      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));
          }
      
       }
      

      【讨论】:

      • 感谢您的回复。我尝试使用 var onList:ArrayCollection = new ArrayCollection();但注意到 ArrayCollection、removeItemAt() 和 addItem 在 Flash 中无法识别。我四处搜索,在一个论坛中看到 ArrayCollection 在 Flex 中使用,并且可以在 AS3 中创建一个 ArrayCollection。我需要更多地研究如何做到这一点,但感谢您的意见。
      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多