views:

24

answers:

1

I have multiple toggle buttons on my flash stage. These toggle buttons are MovieClips. If a user clicks on one of the MovieClips, I want to be able to find a way to go through the Object below--get the appropriate value based on what the User clicked--and store that value in a new value called var powerData which will then be passed to a function to perform calculations.

           var houseArray:Object = {lightA:"1", 
                                    lightB:"1", 
                                    lightC: "1"
                                    lightD: "1"
                                    lightE: "1"
                                    comp: "2"
                                    tv: "3"
                                    stove: "4"
                                    laundry: "5"};

For example: If the user clicked on "Comp" MovieClip then--> var powerData = 2; (because in houseArray:Object--> comp: "2")

A: 

You will need to name your MovieClips instances after the properties of the houseArray object. In your example, the Comp instance should be named "comp" , the TV "tv" and so on...

  //Could also be , private var comp:Comp;
  private var comp:MovieClip;

  private function init():void
  {
      comp = new Comp();
      comp.name = "comp";
   }

  private function mouseClickHandler(event:MouseEvent):void
  {
      var buttonName:String = event.currentTarget.name;

      //since the houseArray property value is a String, the result needs 
      //to be typed as int( or Number, depending on context )
      var powerData:int = int( houseArray[buttonName] );

      performCalculations(powerData);
  }

  private function performCalculations(value:int):void
  {
      //your code here
  }

I assume you have good reasons to have the properties in houseArray as Strings...

PatrickS
My mistake, I need to remove the double quotes. The data type of the properties inside houseArray should really be integers.
jc70
@PatrickS thank you for replying. Response was helpful. Using the same concept, I'll be writing var powerData:int = houseArray[e.currentTarget.name.toLowerCase()]; instead of var powerData:int = int( houseArray[buttonName] ); . But I believe it follows the same concept you are showing.
jc70