...

View Full Version : something screwy with function



the_bob
06-13-2004, 08:15 PM
ok, one of my functions doesn't work. not only is it not working, it is also causing the text in my text boxes to not load. here is the whole page



<html>
<head>
<title>Wishing Well - Beta Version 0.1.2</title>

<h1 align="center"><font size = "5" font="Verdana">Wishing Well - Beta Version 0.1.2</font></h1><br>

<script LANGUAGE="Javascript" type="text/javascript">

var health = 50;
maxhealth = 50; //-- Player Stats
var strength = 15;
var defence = 5;
var level = 1;
var exp = 0;
var expntl = 50;
var gold = 5;

var mhealth=0; //-- Monster Stats
var mstrength=0;
var mdefence=0;

var currentcity="Barino";
var currentmonster=1; //--Miscellaneous Varibales
var moron = "...moron... <_<";
var counter = 0;




function levelup() {
if (exp >= expntl) {
exp = exp - expntl;
expntl = expntl * 2;
level = level + 1;
health = health + (10 * level);
maxhealth = maxhealth + (10 * level);
strength = strength + (5 * level);
defence = defence + (5 * level);
alert("Health: " + health);
alert("Strength: " + strength);
alert("Defence: " + defence);
} else {
alert("You do not yet have the experience to level up.");
alert(moron);
}}



function revive() {
if (health < 1) {
health = maxhealth
alert("Health: " + health);
} else {
alert("You are not dead.");
alert(moron);
}
}
ID=window.setTimeout("Update();",20);
function Update() {
counter ++;
document.form1.health.value="Health: " + health + "/" + maxhealth;
document.form1.strength.value="Strength: " + strength;
document.form1.defence.value="Defence: " + defence;
document.form1.level.value="Level: " + level;
document.form1.exp.value="EXP: " + exp + "/" + expntl;
document.form1.currentcity.value="Current City: " + currentcity;
document.form1.gold.value="Gold: " + gold;
ID=window.setTimeout("Update();",20);

}


function inn() {
if (gold >= 20) {
gold = gold - 20;
health = maxhealth
alert("Health: " + health + "/" + maxhealth);
alert("Gold: " + gold);
} else {
alert("You, sadly, no not have the funds to rest at the inn.");
alert(moron);
}}



function bossbattle(){
if (health > 0) {
if (currentcity=="Barino"){
bhealth=75;
bstrength=35;
bdefence=20;
if ((strength - bdefence) > 0) && ((bstrength - defence) > 0) {
bhealth = bhealth - (strength - bdefence);
alert("You attacked the boss for " + (strength - bdefence) + " damage.");
health = health - (bstrength - defence);
alert("The boss attacks you for " + (bstrength - defence) + " damage.");
}
if ((strength - bdefence) < 0) {
alert("The boss laughs at your pitifullness. He lets you leave without harming you.");
alert("You should train before facing him again.");
}
if ((bstrength - defence) < 0) {
alert("You slay the boss with your first attack.");
alert("You sure are strong...");
exp = exp + 50;
gold = gold + 100;
if (exp >= expntl) {
levelup();
}
}
}}}








</script>
</head>
<body>

<form NAME="form1">
<input type="button" value="Boss Battle" onclick="bossbattle()">
<input type="button" value="Random Battle" onclick="randombattle()">
<br><br>
<input type="button" value="Visit Inn" onclick="inn()">
<input type="button" value="Revive" onclick="revive()">
<hr>
<input type="text" NAME="health" SIZE="40">
<input type="text" NAME="currentcity" SIZE="40"><br>
<input type="text" NAME="strength" SIZE="40">
<input type="text" NAME="defence" SIZE="40"><br>

<input type="text" NAME="level" SIZE="40">
<input type="text" NAME="exp" SIZE="40"><br>
<input type="text" NAME="gold" SIZE="40">

</form>

<hr>

<img SRC="wishingmap.gif">

</body>
</html>


if i take this out, the textboxes work fine



function bossbattle(){
if (health > 0) {
if (currentcity=="Barino"){
bhealth=75;
bstrength=35;
bdefence=20;
if ((strength - bdefence) > 0) && ((bstrength - defence) > 0) {
bhealth = bhealth - (strength - bdefence);
alert("You attacked the boss for " + (strength - bdefence) + " damage.");
health = health - (bstrength - defence);
alert("The boss attacks you for " + (bstrength - defence) + " damage.");
}
if ((strength - bdefence) < 0) {
alert("The boss laughs at your pitifullness. He lets you leave without harming you.");
alert("You should train before facing him again.");
}
if ((bstrength - defence) < 0) {
alert("You slay the boss with your first attack.");
alert("You sure are strong...");
exp = exp + 50;
gold = gold + 100;
if (exp >= expntl) {
levelup();
}
}
}}}


where did i go wrong? IE says it is a syntax error

scroots
06-13-2004, 08:20 PM
} else { should be

}else{

in your second exampl i couldn't spot any else statmenets.

scroots

the_bob
06-13-2004, 11:14 PM
oh, thanx, i didn't know the spaces mattered

glenngv
06-14-2004, 06:07 AM
Space doesn't matter in that case. If you indent the lines properly, you will clearly see if you have syntax errors. Also you should enable javascript error notification (IE) or open Javascript Console (NS) to see if there are js errors.

the_bob
06-14-2004, 08:02 PM
yes, IE's error message said it was a syntax error
don't have netscape...

this is really making me mad. i'm used to functions not working, but never had it mess up the rest of my page before

the_bob
06-14-2004, 08:04 PM
how do I indent lines properly? is there a tutorial somewhere online, or should i just space it out more?

kansel
06-14-2004, 09:22 PM
Here is your culprit. Spaces don't matter, don't change the way you code just because someone tells you.

92: if ((strength - bdefence) > 0) && ((bstrength - defence) > 0) {

Remove these two and that syntax error goes away. The problem is that the outer parenthesis must go from the 'if' to the '{'. The && should be contained therein.

kansel
06-14-2004, 09:40 PM
This game looks really cool. I'm interested in seeing it when it gets closer to finished (noticed the randombattle function is missing).

It probably doesn't matter now, for testing purposes, but when you're ready to take the "beta" off, you might want to think about adding a "status" textarea where you can push information instead of using alert().

One game that I particularly liked was Baldo's Gate (http://www.shuesplace.iwarp.com/baldosgate.html) (not related to Baldur's Gate) but it used alert() to relay information that made playing the game a little choppy (and loud on browsers that DING when the alert appears).

Note: The url i posted is not the original author of that game. I have never found the author's original page. I first saw it on JavaScripts.com but their search engine is one of the worst in the world. The one I posted just happened to come up on google.

Note 2: The original author of Baldo's Gate created a sequel that did away with the alerts resulting in much smoother play. The sequel's name is Baldo's Gate 2 and that same person also wrote another jsRPG called Shadow Lurker.

the_bob
06-15-2004, 01:35 AM
thanx kansel, i'll have to remember that parenthis thing.
and you are right, the alerts are bad. I'll probably take them off once i get it working good
thanks again!
-the_bob

glenngv
06-15-2004, 03:14 AM
how do I indent lines properly? is there a tutorial somewhere online, or should i just space it out more?
You just use tab in the editor you're using.


function inn() {
if (gold >= 20) {
gold = gold - 20;
health = maxhealth
alert("Health: " + health + "/" + maxhealth);
alert("Gold: " + gold);
}
else {
alert("You, sadly, no not have the funds to rest at the inn.");
alert(moron);
}
}

Does that look better and clearer? It makes syntax errors such as missing } and the like easier to spot. It's for your own good (and not for anything else) that I advise you to use tabs in your codes. :)

the_bob
06-15-2004, 03:13 PM
that does look alot better
thanks glenn

Roy Sinclair
06-15-2004, 05:28 PM
Kansel is right that you shouldn't change the way you code just because someone tells you to. However, when you can see the advantages of changing how you code because someone shows you a better way...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum