A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Dynamic dice?

  1. #1
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26

    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?
    Last edited by --Fusiox--; 11-13-2009 at 02:54 PM.

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Code:
    stop();
    
    dieSides = Number(sidesInput.text); 
    wepBonus = Number(bonusInput.text);
    
    btnRoll.onRelease = function(){
    	die = Math.round(Math.random() * diceSides);
                 die += wepBonus;
    } // end roll
    Last edited by ImprisonedPride; 11-10-2009 at 08:27 PM.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  3. #3
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    I would suggest using Math.ceil instead of Math.round, as dice rolls start from 1, not 0.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  4. #4
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26
    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

  5. #5
    www.flashdice.com
    Join Date
    Nov 2009
    Location
    Canada
    Posts
    1
    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.

  6. #6
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26
    @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:
    dynamicDice cs3.fla

    Thanks for all your help!
    -Fusiox

  7. #7
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    -_-

    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
    Last edited by bluemagica; 11-13-2009 at 04:02 PM.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center