jc70
jc70

Reputation: 247

Add dynamic instances of MovieClips to an array

I'm trying to add an instance of a MovieClip inside an array. Inside the House Class is a property called HouseObjects. Inside that array, I created a Comp and a Light class. MovieClips are dynamically placed on the stage, via linkage. The MovieClips also act as "toggle buttons." If the button state is ON, value is 1. If the button state if OFF, value is 0.

If the value is 1, I am trying to add MovieClip instance inside the onList Array. Inside that array will be all the instances that have a button state ON.

I created a property called objSelect.

var objSelect:Object;

That variable holds the currentTarget selected. I'm trying to pass it to function trackItems to either push/pop it in the onList array, based on the button status.

I receive an error for this line: onList.pop(objSelect); Incorrect number of arguments. Expected no more than 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]);
    }

 }

}

Upvotes: 1

Views: 2271

Answers (5)

PatrickS
PatrickS

Reputation: 9572

You don't need to loop thru an Array in order to find the element to remove, simply use the indexOf method:

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

I would suggest to only add the name of an object to the onList Array, it makes comparison more straightforward and less prone to errors

//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 );

then you can update the Stage like this:

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

}

Upvotes: 1

jc70
jc70

Reputation: 247

Created a function that finds the item that needs to be removed, and passed in in objSelect. When item was found, then used 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);
            }
        }

}

Upvotes: 1

Eugene
Eugene

Reputation: 2216

my advice is to use more advanced collections such as
http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html#methodSummary

here is modified code:

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

 }

Upvotes: 0

akonsu
akonsu

Reputation: 29536

pop removes the last item of the array. if you want to remove the given item, you will need to shift the tail of the array up at the position of the element you want to remove

Upvotes: 0

alxx
alxx

Reputation: 9897

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

Pop removes last element from array, if you want to remove specific element, use Array.splice with deleteCount == 0.

Upvotes: 1

Related Questions