Web Analytics Made Easy -
StatCounter integer usage - CodingForum

Announcement

Collapse
No announcement yet.

integer usage

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • integer usage

    ok, i'm trying to make a simple text based game, but i keep getting an error when I try to call a function. Here's my code..

    Code:
    <script>
    var health=15;
    var strength=30;
    var defence=10;
    var m1health=16;
    var m1strength=30;
    var m1defence=4;
    
    funtion attack()  {
    	if health > 0  {
    		 alert("You attack the monster for " + strength - m1defence + "damage.");
    		 alert("You have " + health + "health left.");	
    		 alert("Your opponent has " + m1health + "left.");	
    
    	}   else    {
    		 alert("You are dead.  You may not attack.");
    }
    </script>
    My guess is that i messed up trying to subtract. I don't recall seeing that in a book or on a website.

    alert("You attack the monster for " + strength - m1defence + "damage.");

    is that the part that is wrong?

  • #2
    Hi there the_bob

    You just have a few ommissions...
    Code:
    <script [color=red]type="text/javascript"[/color]>
    var health=15;
    var strength=30;
    var defence=10;
    var m1health=16;
    var m1strength=30;
    var m1defence=4;
    fun[color=red]c[/color]tion attack()  {
    	if[color=red]( [/color]health > 0 [color=red])[/color] {
    		 alert("You attack the monster for " +[color=red] ([/color]strength - m1defence [color=red])[/color]+ " damage.");
    		 alert("You have " + health + " health left.");	
    		 alert("Your opponent has " + m1health + " left.");	
    
    	}   else    {
    		 alert("You are dead.  You may not attack.");
    }
    [color=red]}[/color]
    </script>
    cthead
    ~ the original bald headed old fart ~

    Comment


    • #3
      thanks coothead, that gets it working, but i just realised i forgot to make it calculate damage I tried this but i get another error.

      Code:
      function attack()  {
      	if( health > 0 ) {
      		 alert("You attack the monster for " + (strength - m1defence )+ " damage.");
      		 alert("Your opponent has " + (m1health - (strength - m1defence)) " left.");	
      
      	}   else    {
      		 alert("You are dead.  You may not attack.");
      }
      }</script>

      Comment


      • #4
        Hi there the_bob,

        Your code was minus a +

        Code:
        alert("Your opponent has " + (m1health - (strength - m1defence))[color=red] +[/color] " left.");
        cthead
        ~ the original bald headed old fart ~

        Comment


        • #5
          *feeling pretty stupid about now*
          thanks coothead, works perfectly


          but now, yet another question; is there any way to change the value of the variable? for example,

          var a = 5
          var b = 8

          if i add a and b, i will get 13. Is there any way to have that change the value of a from 5 to 13?
          Last edited by the_bob; Feb 20, 2004, 05:10 PM.

          Comment


          • #6
            Hi there the_bob,

            var a=5;
            var b=8;
            var a=a+b;

            cthead
            ~ the original bald headed old fart ~

            Comment


            • #7
              this should also work:
              a+=b

              http://www.bluephoenix.uni.cc/

              Comment


              • #8
                ok, I changed the code to

                Code:
                alert("You attack the monster for " + (strength - m1defence )+ " damage.");
                
                var m1health = (m1health - (strength - m1defence));		
                alert("Your opponent has " + m1health + " left.");
                now it is saying "Your opponent has NaN left."
                thats not cool

                Comment


                • #9
                  Code:
                  var m1health = (m1health - (strength - m1defence));
                  You're declaring then using the variable mlhealth all on one statement. This doesn't make sense.

                  Actually you're redeclaring an existing variable. Get rid of the var

                  Comment


                  • #10
                    Ahh, that makes sence. and it works!
                    thanks

                    Comment

                    Working...
                    X