How to return a value from a function in AS3? Then store than value in a variable to pass it to another function?
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,lig开发者_如何转开发htD: 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;
}
}
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...
精彩评论