View Full Version : addition?
dayglowdave
March 29th, 2005, 10:18 PM
has anyone had any problems with addition? I'm trying to get two numbers to add and instead it is concatenating them together as a string, however when I multiply, divide, or even subtract them it works fine.
reduakari
March 29th, 2005, 10:32 PM
hmm...
dont know wat it means..
var num1 = 0, num2 = 0;
var sum = 0;
sum = num1 + num2;
hope i got your meaning right.
dayglowdave
March 29th, 2005, 10:38 PM
ok here is an example:
valTot = 1 + 2
returns 12 not 3.
where val1 is a number arrived at from a previous calculation and val2 is a value from an input textfield. When I try multiplying I get the correct answer when I try divide it is correct and even when I subtract. The addition is the only thing hanging me up on this one. Which really makes no sense at all.
lordkiller
March 30th, 2005, 12:42 PM
Your variables are treated by flash like strings... the + operator is defined for strings the others are not :)
The only thing that u can do is transforming them to numbers:
result = Number(var1) + Number(var2);
Try, maybee the second Number(var2) is not necesary...
For mc named as "11" use eval(11).gotoAndStop(Number(jumpTo));
dayglowdave
March 30th, 2005, 05:03 PM
thanks for the advice. I'll try it when I get home and let you know.
I just had a thought. Would Math.add be able to solve the problem?
-d
dayglowdave
March 31st, 2005, 08:57 PM
ok here is my source and swf (mx2004)... I thought I had finally figured out this thing with the
fate = fate + (1 * modDi); line but there are erroneous errors within the program. For instance, it rolls things like -2 when I roll 2 dice with a -3 modifier etc... I know it is just another dice roller, but at least for a noob like me it is a step up from 'Hello World!'. I'm hoping to learn the math functions better this way. So if anyone has any thoughts, I would love to hear them.
norie
March 31st, 2005, 09:16 PM
trace(_root.sideDi);
trace(_root.numDi);
fate = Math.floor(Math.random() * (_root.sideDi * _root.numDi)) + 1;
all your vars are undefined. Probably why you're getting NaN (Constant for 'Not a Number')
norie
March 31st, 2005, 09:21 PM
on (press) {
var modDi:Number, sideDi:Number, numDi:Number, fate:Number, rollRes:Number; //insure date integrity
fate = numDi = modDi = sideDi = 0; //initialize variables
fate = Math.floor(Math.random() * (sideDi * numDi)) + 1;
fate = fate + (1 * modDi);
rollRes = fate;
}
not sure what you're doing but here's some code to at least get you started
dayglowdave
March 31st, 2005, 09:26 PM
all your vars are undefined. Probably why you're getting NaN (Constant for 'Not a Number')[/QUOTE]
I'm not getting NaN. My variables are null and awaiting values through the inputfields(dicepool, face value etc.) then when you press the roll button it initiates the calculation with those values. basically I'm trying to make a dice rolling calculator. however my math is wrong somewhere because I keep getting impossible rolls.
best regards :)
Dave
lordkiller
April 1st, 2005, 01:37 PM
If u take "values" over input fields they are taken as strings :(, no mater if u define var1 = 0; as integer, so transform the var1 = "111" to the value 111 with parseInt, a function what is in the MX but not documented.... (well know in JAVA)
Search this forum for "parseInt", I wrote sometimes ago my adventures with (is on the end of a discusion about behavoir of NaN values)
dayglowdave
April 1st, 2005, 03:55 PM
The problem is not that it is outputting strings anymore, I fixed that rather simply by forcing AS to recognize the last number 'modDi' as a number and not as a string by forcing it to multiply by one like so:
fate = fate + ( 1 * modDi );
Remembering that whatever is in parenthesis gets done first, this solution just came to me. Now the problem is that my math is faulty. I want it to take a user specified kind of die (i.e. D4, D6, D10, D12, D20, D100) and multiply that by the user specified number of dice in a dicepool and finally modify the result of that roll by a user-define modifier (e.g. +2, -3, etc.) What my math is ACTUALLY telling AS to do is take all the sides of a die and multiply that by the quantity of dice, which is not what I'm intending, and thus is the source of the rolling errors. I need to loop it I think. Now I'm trying to figure out how to do that. :)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.