PDA

View Full Version : Personal Javascript RPG "calculator" help

kenderbard
01-23-2009, 07:44 AM
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!

Philip M
01-23-2009, 08:15 AM
This ought to give you some guidance:-

<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) {
hitPoints = hitPoints - randy;
}
else {
}

}
</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

kenderbard
01-23-2009, 10:49 AM
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.

function hpoints1() {
if (randy <= ToHit) {
hitPoints = HP - Damage2;
}
else {
}

}

function hpoints2() {
if (randy <= ToHit2) {
hitPoints2 = HP2 - Damage;
}
else {
}

}

}

Philip M
01-23-2009, 11:11 AM
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.

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

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

kenderbard
01-23-2009, 06:07 PM
Hm, tried adding that alert and nothing came up. Here's the buttons.

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

Philip M
01-23-2009, 06:36 PM
Looking at your script, you have one closing brace too many:-

}
}

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?

kenderbard
01-23-2009, 08:54 PM
EDIT: I fixed it. :) Found a minor error in another function I added.

Thank you very much for all your help!

kenderbard
01-25-2009, 11:06 AM
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...

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.

Philip M
01-25-2009, 11:10 AM
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'.

kenderbard
01-25-2009, 11:31 AM
I think so...

<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.

Philip M
01-25-2009, 11:56 AM
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()"

kenderbard
01-25-2009, 05:48 PM
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?

Philip M
01-25-2009, 05:56 PM
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.