views:

89

answers:

5

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

 }

}

+1  A: 

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.

alxx
@alxx, i tried passing objSelect inside pop(); --but receiving an error, saying incorrect number of arguments, expected 0.
jc70
@crewof1: right, this is what alxx is saying: you cannot do that.
akonsu
@alxx and @akonsu: is it possible to add an instance of a movieclip into an array?
jc70
yes, to add something to an array, use push()
akonsu
@akonsu i understand the difference between push and pop. the problem i'm running into, is being able to pass the variable objSelect into push().
jc70
you cannot pass a parameter to pop. it does not take any parameters. it removes the last element. it does not need a parameter. but push on the other hand, does need a parameter. the thing that you want to push. please formulate your question.
akonsu
@aksonu: that makes sense about pop, it'll only remove the last item of the array. which will be an issue for what i want to do. for now, i'll try to get pop (adding items to the array) to work. my goal is to add items that are ON to the onList array.
jc70
you lost me. i do not know what else to suggest...
akonsu
A: 

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

akonsu
A: 

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

 }
Eugene
@Eugene thanks for the response. i tried using var onList:ArrayCollection = new ArrayCollection(); but noticed that ArrayCollection, removeItemAt(), and addItem was not recognized in Flash. i googled around, and saw in a forum that ArrayCollection is used in Flex, and that it is possible to create an ArrayCollection in AS3. i'll need to look more into how to do that, but thank you for that input.
jc70
+1  A: 

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

}
jc70
You don't need to loop thru the Array , simply use the indexOf method: var index:int = onList.indexOf(objSelect); onList.splice( index , 1 );
PatrickS
+1  A: 

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

}
PatrickS