5 Replies Latest reply on May 13, 2009 11:11 AM by wwscoper

# update "money" variable with a function

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.

• ###### 1. Re: update "money" variable with a function

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

• ###### 2. Re: update "money" variable with a function

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;

• ###### 3. Re: update "money" variable with a function

Thanks!

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

I appreciate the help!

• ###### 4. Re: update "money" variable with a function

You're welcome

• ###### 5. Re: update "money" variable with a function

I have another question on this same project.

The bets for the game are being placed by dragging a chip to a slot on the table. There are six slots, with numbers 1-6, 6-12, 12-18, etc....

I have a \$25 chip and a \$5 chip. I am using _droptarget to detect when a chip has been placed on one of the slots. If a \$25 chip is placed on "slot1",

slot1Bet += 25;

Now the tricky part...if I move the chip from slot1 to another slot before the roulette wheel is spun, I need to change the bets so that "slot1 -=  25" and another slot get the bet added. I'm calulating the final results by using something like this:

if (winningNumber<= 6){

return Number(money) + Number(slot1Bet) - Number(slot2Bet) - Number(slot3Bet) - Number(slot4Bet) - Number(slot5Bet) - Number(slot6Bet);

Any ideas?

Thanks!