Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Personal Javascript RPG "calculator" help

    I'm trying to make a calculator of sorts using javascript for an RPG. The idea is that I should be able to pull up a web page and plug in in various stats for things like strength, defense, speed, etc and click a couple buttons so that it calculates how much damage (if any) Dragon 1 does to Dragon 2 and vice versa, keeping a running tally of a steadily decreasing hit point score.

    The part I'm having trouble with is that I'm not sure how to code a button so that it 1) activates the random number variable, 2) checks the number generated against a "To Hit" number to either make it a hit or a miss, and 3) proceed to subtract the damage variable from the hit point variable if it is a hit. A bonus would be if it told me the HP value after doing all this math in an alert.

    I'm not sure if anyone who's willing to help wants to see the code I have or not, so I'll refrain from posting it until requested. Thanks for reading!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    This ought to give you some guidance:-

    Code:
    <input type = "button" name = "but1" value = "Evaluate Hit Points" onclick = "hpoints()">
    
    <script type = "text/javascript">
    
    var hitPoints = 100;
    var hitOrMiss = 20;  // i.e 20% chance of a hit
    function hpoints() {
    var randy = Math.floor(Math.random() * 100);
    if (randy <= hitOrMiss) {
    alert ("You scored a hit!");
    hitPoints = hitPoints - randy;
    }
    else {
    alert ("Bad luck! You missed.")
    }
    alert ("Your opponent's hit-point value is now: " + hitPoints)
    
    }
    </script>
    You could make the chance of a hit higher (say 60%) but deduct only parseInt(randy/10) points per hit.


    Quizmaster: In seafood on a restaurant menu, the French word "poisson" translates into English as what?
    Contestant: Chicken

  • Users who have thanked Philip M for this post:

    kenderbard (01-23-2009)

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you very much for your help! It took me leaps and bounds to getting closer to my goal.

    I couldn't use it exactly because I already had several variables set up. Now the problem is that I'll get all the prompts to fill in the proper variable amounts but when I click the buttons, nothing happens. Here's the modified code below in case I might have broken something.

    I kept the Randy part but moved it up earlier in the script so didn't include it in this repaste.

    Code:
    function hpoints1() {
    if (randy <= ToHit) {
    alert ("You scored a hit!");
    hitPoints = HP - Damage2;
    }
    else {
    alert ("Bad luck! You missed.");
    }
    alert ("Your opponent's hit-point value is now: " + hitPoints);
    
    }
    
    function hpoints2() {
    if (randy <= ToHit2) {
    alert ("You scored a hit!");
    hitPoints2 = HP2 - Damage;
    }
    else {
    alert ("Bad luck! You missed.");
    }
    alert ("Your opponent's hit-point value is now: " + hitPoints2);
    
    }
    
    }

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by kenderbard View Post
    Now the problem is that I'll get all the prompts to fill in the proper variable amounts but when I click the buttons, nothing happens.
    Show your HTML mark-up please, i.e buttons

    You can trace what the problem is if you add an alert, so:-

    function hpoints1() {
    alert ("Here we are at hppoints1");

  • #5
    New Coder
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hm, tried adding that alert and nothing came up. Here's the buttons.

    Code:
      <input type = "button" name = "but1" value = "Evaluate Hit Points" onclick = "hpoints1()"/>
      <input type = "button" name = "but2" value = "Evaluate Hit Points 2" onclick = "hpoints2()"/>

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Looking at your script, you have one closing brace too many:-

    alert ("Your opponent's hit-point value is now: " + hitPoints2);
    }
    }

    That prevents the script from executing.

    I kept the Randy part but moved it up earlier in the script so didn't include it in this repaste.
    Surely you want to generate a different random number each time one of the scripts is called?

    hitPoints = HP - Damage2; Where are these variables declared?


    If you turn on error reporting in your browser that will help you identify problems better.
    Last edited by Philip M; 01-23-2009 at 07:40 PM.

  • #7
    New Coder
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    EDIT: I fixed it. Found a minor error in another function I added.

    Thank you very much for all your help!
    Last edited by kenderbard; 01-23-2009 at 10:18 PM.

  • #8
    New Coder
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Well I got the calculator to work beautifully. So all the tricky little parts are no longer a problem. Now I want to make it a little more user friendly and that's causing a new problem.

    As it stands I have a series of variables that are defined by prompts that load when the page is opened. I'd rather have it so the user can just plug in numbers in a text field. Yet when I had the variables rewritten to get their value from the proper form...

    Code:
    var name = document.form1.name1.value;
    ..as an example, I get the "form1 is not defined" error and nothing runs in my later functions that rely on these variables.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Well, is form1 defined?

    Note that in Internet Explorer, names and IDs are global variables and thus you should NEVER use a global variable or function name which is the same as an HTML element name or ID. You should also avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/attributes such as 'name' or 'id' or 'value' or 'text' or 'checked' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.

  • #10
    New Coder
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I think so...

    Code:
    <form name="form1">
    I've used this set up before, so I'm not sure why it's not working. The only difference is the last time I did this, all the variables that were taking their values from a form were inside of a function whereas these are not.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Well, they must be in a function, otherwise the script runs immediately on load:-


    <script type = "text/javascript">
    var ItemName = document.form1.name1.value; // don't use name as a variable
    //which does not exist yet. So returns undefined.

    Within a function:-

    function whatever() {
    var ItemName = document.form1.name1.value;
    // takes value from form when activated by something like button onclick = "whatever()"
    Last edited by Philip M; 01-25-2009 at 01:01 PM.

  • #12
    New Coder
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    EDIT: And now I know that these things are case sensitive. Heh. Took your advice on changing the name thing.

    By the way is there a way to delete our own posts so I stop double posting cause an earlier one got resolved?

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by kenderbard View Post
    By the way is there a way to delete our own posts so I stop double posting cause an earlier one got resolved?
    Yes, Edit then Go Advanced. (There is a time limit of 2-3 days).

    But in general it is not a good idea to delete your posts as it may well make subsequent posts by others incomprehensible. If an issue is resolved others may be interested to learn the solution.


  •  

    Posting Permissions

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