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 12 of 12
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    something screwy with function

    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

    Code:
    <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

    Code:
    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

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    } else { should be

    }else{

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

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #3
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh, thanx, i didn't know the spaces mattered

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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.

  • #5
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how do I indent lines properly? is there a tutorial somewhere online, or should i just space it out more?

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    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 (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.

  • #9
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by the_bob
    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.
    Code:
    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.

  • #11
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that does look alot better
    thanks glenn

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    Check out the Forum Search. It's the short path to getting great results from this forum.


  •  

    Posting Permissions

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