...

View Full Version : Personal Javascript RPG "calculator" help



kenderbard
01-23-2009, 08: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, 09: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) {
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

kenderbard
01-23-2009, 11: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) {
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);

}

}

Philip M
01-23-2009, 12:11 PM
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");

kenderbard
01-23-2009, 07: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, 07:36 PM
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.

kenderbard
01-23-2009, 09: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, 12:06 PM
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, 12:10 PM
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, 12:31 PM
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, 12:56 PM
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, 06: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, 06: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum