Be a Supporter!
apf42589
apf42589
  • Member since: Oct. 21, 2006
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
As3 Functions Mar. 28th, 2012 @ 06:32 PM Reply

hey im trying to call a function by using a variable.

/////Slot1 = "PineLogs"
function ItemSlot1Click (e:MouseEvent):void {
Slot1 = "Empty"
Slot1(); ----> I need this to some how call the PineLogs();
}

function PineLogs():void {
Player_Gold+= 15
}

any ideas? I tried ("Slot1")(); "Slot1"(); =\

Mattster
Mattster
  • Member since: Nov. 21, 2007
  • Offline.
Forum Stats
Member
Level 20
Game Developer
Response to As3 Functions Mar. 28th, 2012 @ 06:45 PM Reply

At 3/28/12 06:32 PM, apf42589 wrote: hey im trying to call a function by using a variable.

/////Slot1 = "PineLogs"
function ItemSlot1Click (e:MouseEvent):void {
Slot1 = "Empty"
Slot1(); ----> I need this to some how call the PineLogs();
}

function PineLogs():void {
Player_Gold+= 15
}

any ideas? I tried ("Slot1")(); "Slot1"(); =\

You can access variables/methods within the class by treating accessing it as you would a property of an Object. This should do the trick:

this["PineLogs"]();
apf42589
apf42589
  • Member since: Oct. 21, 2006
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As3 Functions Mar. 28th, 2012 @ 06:59 PM Reply

You can access variables/methods within the class by treating accessing it as you would a property of an Object. This should do the trick:

this["PineLogs"]();

I can access it by using that yes. But I need to use the Slot1 variable so when you click the slot(which happens to be whatever item is in it, in this case PineLogs) this way i dont have to check what item is in, it looks at the variable name and goes to that variable name's function.

this["Slot1"](); doesnt work =\ even though Slot1 is == PineLogs

apf42589
apf42589
  • Member since: Oct. 21, 2006
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As3 Functions Mar. 28th, 2012 @ 07:02 PM Reply

Got it, thank you soo much lol

this[Slot1](); //calls whatever variable Slot1, and finds the matching function.