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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Please help me with another thing....

    I need help with another code...

    This is my HTML:

    Code:
    <html>
    <head>
    <script language="JavaScript">
    var exp = 0;
    var lvl = 1;
    var monsterhp = 0;
    
    function letamonster() {
    	// random
    	var r=Math.random()
    	if (r<0.1)              // 1
    	{
    	var monsternamn = "Råtta"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 10
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (r>0.1 && r<0.3) //2
    	{
    	var monsternamn = "Padda"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 7
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (r>0.2 && r<0.4) //3
    	{
    	var monsternamn = "Groda"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 5
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (r>0.3 && r<0.5) //4
    	{
    	var monsternamn = "Vägg"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 100
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (r>0.4 && r<0.6) //5
    	{
    	var monsternamn = "Gris"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 14
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (r>0.5 && r<0.7) //6
    	{
    	var monsternamn = "Noshörning"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 55
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (r>0.6 && r<0.8) //7
    	{
    	var monsternamn = "Älg"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 43
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (r>0.7 && r<0.9) //8
    	{
    	var monsternamn = "Fågel"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 10
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (r>0.8)         // 9
    	{
    	var monsternamn = "Mask"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 1
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	document.getElementById('letafiender').disabled=true;
    }
    
    
    function anfall() {
    	// random
    	var a=Math.random()
    	if (a<0.1)              // 1
    	{
    	var monsterhp = monsterhp - 2
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (a>0.1 && a<0.3) //2
    	{
    	var monsterhp = monsterhp - 1
    	document.getElementById('txtmonsterhp').value=monsterhp;	
    	}
    	else if (a>0.2 && a<0.4) //3
    	{
    	var monsterhp = monsterhp - 5
    	document.getElementById('txtmonsterhp').value=monsterhp;	
    	}
    	else if (a>0.3 && a<0.5) //4
    	{
    	var monsterhp = monsterhp - 3
    	document.getElementById('txtmonsterhp').value=monsterhp;	
    	}
    	else if (a>0.4 && a<0.6) //5
    	{
    	var monsterhp = monsterhp - 4
    	document.getElementById('txtmonsterhp').value=monsterhp;	
    	}
    	else if (a>0.5 && a<0.7) //6
    	{
    	var monsterhp = monsterhp - 7
    	document.getElementById('txtmonsterhp').value=monsterhp;	
    	}
    	else if (a>0.6 && a<0.8) //7
    	{
    	var monsterhp = monsterhp - 4
    	document.getElementById('txtmonsterhp').value=monsterhp;	
    	}
    	else if (a>0.7 && a<0.9) //8
    	{
    	var monsterhp = monsterhp - 1
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (a>0.8)         // 9
    	{
    	var monsterhp = monsterhp - 10
    	document.getElementById('txtmonsterhp').value=monsterhp;	
    	}
    	if (monsterhp < 0)
    	{
    	alert('du vann!')
    	}
    }
    
    function getexp() {
    	exp = exp + 5;
    	document.getElementById('txtexp').value=exp;
    	if (exp === 20) {
    	alert('lvl up!');
    	exp = 0;
    	document.getElementById('txtexp').value=exp;
    	}
    }
    
    </script>
    
    <TITLE>
    JavaScript test
    </TITLE>
    </head>
    <body>
    <table>
    <tr>
    <td>HP:<input type="text" id="txthp" name="txthp" value="10" size="2" readonly></td>
    
    <td>EXP:<input type="text" id="txtexp" name="txtexp" value="0" size="2" readonly></td>
    
    /* <td>$:<input type="text" id="txtgp" name="txtgp" value="0" size="2" readonly></td> */
    
    </tr>
    </table>
    
    
    <input type="button" name="letafiender" id="letafiender" value="Leta fiender!" onclick="letamonster()">
    <BR>
    <table><tr>
    <td>
    Monstrets namn: <input type="text" name="txtmonsternamn" id="txtmonsternamn" value="" size="30" readonly>
    </td>
    <td>Monstrets HP: <input type="text" name="txtmonsterhp" id="txtmonsterhp" value="" size="3" readonly>
    </td>
    </tr>
    <tr><td><input type="button" name="anfall" id="anfall" value="Anfall!" onclick="anfall()"></table>
    </body>
    </html>

    What it shall do is the following:
    First, when you click the button "Leta monster", it should come up a random monster name in the textbox "monstrets namn", and a HP value should come up in the textbox "monstrets HP".
    Then the button is disabled, because you have to fight that monster, not go searching for another one.

    This far all is well, here is my problem:
    When you click the button "anfall", the monster should be hurt with a random damage number.
    But, when I click the "anfall"-button, the monster's HP goes "NaN".

    Please, can you help me and tell me what's wrong?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Your problem is here:-

    var monsterhp = monsterhp - 2

    var declares a new variable. You have already declared
    var monsterhp = 0;

    Delete the var before all instances of monsterhp.

    Also best to declare monsternamn at the ouset as
    var monsternamn = "";
    and then delete the var in all the instances of
    var monsternamn = "Padda" etc.

    Is the player supposed to attack the monster repeatedly? I have just reduced Gris to -57 hit points by bashing him/her/it repeatedly!
    Last edited by Philip M; 06-13-2007 at 12:04 PM.

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help!
    I did'nt know "var" just declared a variable, I thought you should use it all the time

    And for your question, I have not had time to make that yet, so it attacks back :P

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Just noticed a detail:-

    var a=Math.random()
    if (a<0.1)
    Change to (in case a evaluates to exactly 0.1)

    var a=Math.random();
    if (a<=0.1) {

    Same for var r=math.random;
    if (r<=0.1) {


  •  

    Posting Permissions

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