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 7 of 7
  1. #1
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Need help with functions etc.

    Hey I'm creating a game that has a 6 buttons below some ingame options etc.
    Now I'm having problems with my levels and its script.

    Below is my script.


    <!-- Begin
    function stats(){
    var armour=3;
    var defense=1;
    var attack=1;
    var total=attack+defense;
    var disp=0;
    if (disp==0) { alert("Your Attack Level Is:"+attack+"\nYour Defense Level Is:"+defense+"\nYour Armour Bonus

    Is:"+armour+"\n_________________\n\nYour Total Level Is:"+total+"\nWith a bonus of "+armour+" Defense Points");}
    }

    function raiseattack(){
    var goupa=0;
    if (goupa==0) { alert("You gained One Level In Attack!"); attack=+1;}
    }
    // End -->

    now when I click a button that has this code:

    <INPUT TYPE=BUTTON VALUE="Train Attack" ONCLICK="raiseattack()">

    The button displays the message that I have leveled up, but doesnt add 1 to the variable atack. so when I view the stats button its still displaying the attack to be one. I need it to raise by one every time this button is clicked.

    PLEASE IF POSSIBLE NO ELEMENTS!!! i havent learnt them yet.

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    </head>
    
    <body onload="stats();">
    <script language="JavaScript" type="text/javascript">
    <!-- Begin
    var armour=3;
    var defense=1;
    var attack=1;
    
    function stats(){
    var total=attack+defense;
    var disp=0;
    if (disp==0) { alert("Your Attack Level Is:"+attack+"\nYour Defense Level Is:"+defense+"\nYour Armour BonusIs:"+armour+"\n_________________\n\nYour Total Level Is:"+total+"\nWith a bonus of "+armour+" Defense Points");}
    }
    
    function raiseattack(){
    var goupa=0;
    if (goupa==0) { attack++; alert("You gained One Level In Attack!\n"+attack); }
    }
    
    
    //-->
    </script>
    <INPUT TYPE=BUTTON VALUE="Train Attack" ONCLICK="raiseattack()">
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by GSimpson
    if (goupa==0) { alert("You gained One Level In Attack!"); attack=+1;}
    }
    Your error is quite simply a problem with your syntax. Instead of attack =+ 1 it should be attack += 1

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    local variable 'attack' needs to be global
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #5
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks everyone, yeah I'm still learning javascript so i probably made sevral mistakes.

  • #6
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Now problem no.2 I am using the script from the first reply.
    A game just wouldn't be any good if you can keep leveling up... thats pretty pathetic.

    So my solution to making it harder is having to wait. So when you click
    the train attack button, I want it to have an alert that counts down 2minutes. and when it counts down to zero it has the alert - you leveled up!!!
    then add one to the variable.
    can someone help me out??

  • #7
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    </head>
    
    <body onload="stats();">
    <script language="JavaScript" type="text/javascript">
    <!-- Begin
    var armour=3;
    var defense=1;
    var attack=1;
    var TO;
    
    function stats(){
    var total=attack+defense;
    var disp=0;
    if (disp==0) { alert("Your Attack Level Is:"+attack+"\nYour Defense Level Is:"+defense+"\nYour Armour BonusIs:"+armour+"\n_________________\n\nYour Total Level Is:"+total+"\nWith a bonus of "+armour+" Defense Points");}
    }
    
    function raiseattack(){
    clearTimeout(TO);
    var goupa=0;
    if (goupa==0) { TO=setTimeout('raiseattack2()',5000); } // 5000 milliSec = 5 seconds
    }
    
    function raiseattack2(){
     attack++;
     alert("You gained One Level In Attack!\n"+attack);
    }
    
    //-->
    </script>
    <INPUT TYPE=BUTTON VALUE="Train Attack" ONCLICK="raiseattack()">
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

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