-
Dynamic dice?
Hey, I'm trying to make a dice-rolling program that can change the amount of sides available, for a small demo. It's for a game I am developing just to see if I can, so I'm in no rush. This is my entire (and very short) source code:
Code:
stop();
dieSides = sidesInput;
wepBonus = bonusInput;
btnRoll.onRelease = function(){
die = Math.round(Math.random() * sidesInput);
die += wepBonus;
} // end roll
As you can see, I have a dynamic text field named "sidesInput" and "bonusInput". I make the variable "dieSides" equal to the text field "sidesInput", as well with "wepBonus" and "bonusInput".
When I click the button "btnRoll", the text field "die" should become equal to a random number multiplied by "sidesInput" rounded to the nearest whole number, with the variable "wepBonus" added to the final result. In theory, this should work for me. But whenever I press "btnRoll" and start off the function that creates the output, all I get is an annoying "NaN" as the output.
How can I fix this?
-
Code:
stop();
dieSides = Number(sidesInput.text);
wepBonus = Number(bonusInput.text);
btnRoll.onRelease = function(){
die = Math.round(Math.random() * diceSides);
die += wepBonus;
} // end roll
-
I would suggest using Math.ceil instead of Math.round, as dice rolls start from 1, not 0.
-
Oh, so it's a code thing, not a logical error, then...
Every time I post something here, I learn a bunch of new things =D
-
I created a random dice rolling program might as well show it since you are talking about it :) http://www.flashdice.com
real simple code used
onClipEvent (load) {
gotoAndStop(random(7)+2);
}
onClipEvent (load) {
this._rotation = random(90)+90;
}
code used to make it selecting a random frame in the movie it all worked out pretty good let me know what you think.
-
1 Attachment(s)
@Cubolution,
Nice dice! (Pretty punny, huh) However, it still is a bit different from mine in the fact that it uses the numbers to go to frames and directions. That I know how to do. However, I'm not manipulating the numbers to go to frames; I'm manipulating the numbers themselves. I'm trying to make the dice have any amount of sides that the user puts in.
Which brings me to my second point...
My God, I'm a noob! Yeah, I've tried your method ImprisonedPride, but it still outputs NaN at the "die" field. I'm very annoyed at Flash right now.
Did you use AS2 or AS3 in your code edit? That is, is the var type Number and the prefix ".text" actually relevant in older code syntaxes? If you used AS3, that would explain it all.
I'm uploading a version of the .fla on the site. Get it here:
Attachment 71106
Thanks for all your help!
-Fusiox
-
-_-
Your code won't work.
1) You are intialising your variables at the start, hence it tries to convert the "type number of..." to number and store that, rather than what the user entered.
2)die is the dynamic textbox's instance name, and you are using it as a variable for calculation and directly inside the textbox's var property as well.....it dosen't work that way. the instance name is just a reference.
http://www.mediafire.com/?ntwmmmzxvzn