Skip to main content
Known Participant
May 13, 2009
Answered

update "money" variable with a function

  • May 13, 2009
  • 2 replies
  • 762 views

Hi all,

I am creating a roullete wheel type game. I am having a problem updating a total when I call a function to add the variables. I have a variable "myMoney" that is initialized at 50. After I spin the wheel, I want to add the bets to the current value of "myMoney." I have another variable, "slot1Bet" that is initialized at 0. I am trying to call the function updateMoney(myMoney) which than should take the current value of "myMoney" and set it equal to itself plus the slot1Bet. The trace statement for "money equals...." returns 500 and so does the updateMoney function. I would appreciate any help solving this problem so the correct value is returned based on the slot bet.

_global.myMoney = 50;

_global.slot1Bet = 0;

trace("updated money equals " + updateMoney(myMoney));

var updateMoney = function(money:Number){

money = money + slot1Bet;

trace("money equals" + money);

trace("myMoney equals" + myMoney)

trace("slot 1 bet equals" + slot1Bet);

return money;

}

***********************************************

I think one problem is that I had some code on a draggable movieclip with a button inside. I've changed that to just be a draggable mc. I also updated the function to:

//Calculate the users new money after the wheel has been spun.

var updateMoney = function(money:Number){

trace("money equals" + money);

trace("myMoney equals" + myMoney)

trace("slot 1 bet equals" + slot1Bet);

return money * money;

}

This function returns the correct value of 2500 but if I change the last line from, "return money*money" to "return money + slot1Bet;" I get 5025. Why is the bet being added to the end of the number and not actually adding to the total.

This topic has been closed for replies.
Correct answer Ned Murphy

I'm assuming your problem is having 50 + 25 returning 5025 instead of 75.

You are dealing between strings and numbers, so it may be best to clarify what you are dealing with in such circumstances.  Try using:

return Number(money + slot1Bet);

OR

return Number(money) + Number(slot1Bet);

OR do the math before you try returning a value...

var returnValue = money + slot1Bet;

return returnValue;

2 replies

Ned Murphy
Ned MurphyCorrect answer
Legend
May 13, 2009

I'm assuming your problem is having 50 + 25 returning 5025 instead of 75.

You are dealing between strings and numbers, so it may be best to clarify what you are dealing with in such circumstances.  Try using:

return Number(money + slot1Bet);

OR

return Number(money) + Number(slot1Bet);

OR do the math before you try returning a value...

var returnValue = money + slot1Bet;

return returnValue;

wwscoperAuthor
Known Participant
May 13, 2009

Thanks!

return Number(money) + Number(slot1Bet);   //this line worked

I appreciate the help!

Ned Murphy
Legend
May 13, 2009

You're welcome

kglad
Community Expert
Community Expert
May 13, 2009

if slot1Bet = 0, why do you expect that myMoney+slotBet would be different that myMoney?