views:

22

answers:

1

I am trying to have the function toggleClick return powerData. I need to store powerData in a variable and have it passed to another function to do calculations.

Inside the function toggleClick, I am assigning powerData to houseArray[e.currentTarget.name.toLowerCase()];

 var powerData:int = houseArray[e.currentTarget.name.toLowerCase()];

I tried just to write return powerData--but it breaks the program.

// function toggleClick

  function toggleClick(e:MouseEvent) {

// Store "Power" Values of House Objects in Array
   var houseArray:Object = {lightA: 1, lightB: 1, lightC: 1,lightD: 1, lightE: 1,
                             comp: 2,
                             tv: 3,
                             stove: 4,
                             laundry: 5};

   // Store User Power in Variable called powerData
   // The currentTarget will equal powerData
   var powerData:int = houseArray[e.currentTarget.name.toLowerCase()];
   //return powerData;

  trace("movieClip Instance Name = " + e.currentTarget); // [object Comp]
  //trace(houseArray[e.currentTarget.name]); // comp
  trace("using currentTarget: " + e.currentTarget.name); // comp
  trace("powerData: " + powerData); // the amount of power that I clicked on
  //trace("houseArray: " + houseArray[0]); // the 0 index of house array
  trace(houseArray[e.currentTarget.name]); // currentTarget inside Array

   // how to find out which object selected

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

}

A: 

Why don't you have powerData defined oustside the function, the value of powerData will be updated after each click

var powerData:int;

function toggleClick(event:MouseEvent):void
{
   powerData = houseArray[e.currentTarget.name.toLowerCase()];

   performCalculation();
}

function performCalculation():void
{
    // you can use the powerData new value here
}

Or you simply pass the powerData value to your function arguments

var powerData:int;

function toggleClick(event:MouseEvent):void
{
   powerData = houseArray[e.currentTarget.name.toLowerCase()];

   performCalculation(powerData);
}

function performCalculation(value:int):void
{
    // access the new value here
}

for "return powerData" to work, your function needs to return a value of int


function toggleClick(event:MouseEvent):int
{
   return powerData;
}

but I'm not sure how that would work within your code though...

PatrickS
@PatrickS thank you for the response. I'm thinking, just like the "Meter Bar" problem--I would need a customEventListener. Inside the customEventListener, the powerData, and button status (On/Off) would be passed. The customEventListener would see if the button status is either On/OFF--in order to see if addition or subtraction is needed--just like the "Meter Bar."
jc70
You could create a class for your HouseObjects , this class would have a status( on/off ) property and a power property. When a HouseObject button is clicked you could then retrieve the values of both properties in your event handler.
PatrickS