joeyoung2509 Posted May 17, 2015 Share Posted May 17, 2015 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><script src="scripts/jquery.js" type="text/javascript"></script><link rel=stylesheet href="scripts/css.css" type="text/css" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>The Game!!</title></head><body><div id="console"><!--MAIN GAME--><p id="userHP2">userHP</p><p id="message_begin">Welcome to the GAME!!.</p><p id="message_enter">You entered the game, well done</p><p id="area_main">Type enter to play.</p><div id="placeholder"></div></div><!--INPUT--><form onsubmit="return false;"><center><input type="text" id="command_line" size="50" autofocus="autofocus" /></center></form></div><p class="copyright"></p><script type="text/javascript" src="scripts/game.js"></script></body></html> Heres The JavaScript which is troubling me; var userHP = "500";var mobHP = 500;var hits = Math.floor(Math.random()*2);$(document).ready(function() { $("#compass").fadeIn(3000); $("#message_begin").fadeIn(3000); $("#area_main").fadeIn(3000); $("#command_line").fadeIn(3000); $("form").submit(function() { var input = $("#command_line").val(); if (input.indexOf("help") > -1) { if (input == "help") { userHP; $("#message_help").clone().insertBefore("#placeholder").fadeIn(1000); } } else if (input.indexOf("enter") > -1) { if (input == "enter") { $("#message_enter").clone().insertBefore("#placeholder").fadeIn(1000); } } else if (input.indexOf("attack") > -1){ if (input == "attack"){ $(userHP - hits).insertBefore("#placeholder").fadeIn(1000); } } I've recently started learning javascript I'm making a text based game and im having trouble printing a variable to my page. Heres my the relevant bit of the code, when the user types attack I want the variable userHP minus hits to be shown on the page. Currently nothing happens when I enter attack What am I doing wrong? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 18, 2015 Share Posted May 18, 2015 $(userHP - hits).insertBefore("#placeholder").fadeIn(1000);That line is passing a number (userHP minus hits) to jQuery. I don't think jQuery does anything when you pass it a number. You would have to create a new element (a span, a div, etc) with the text that you want in it, and then you can pass that element to jQuery. Link to comment Share on other sites More sharing options...
joeyoung2509 Posted May 19, 2015 Author Share Posted May 19, 2015 (edited) well I actually managed to solve my problem. I changed else if (input.indexOf("attack") > -1){ if (input == "attack"){ $(userHP - hits).insertBefore("#placeholder").fadeIn(1000); to else if (input.indexOf("attack") > -1) { if (input == "attack"){ $("#message_attack").html(mobHP -= DMG).clone().insertBefore("#placeholder").fadeIn(1000); Looks like it needed the .html and to be assigned to a DIV. Edited May 19, 2015 by joeyoung2509 Link to comment Share on other sites More sharing options...
Ingolme Posted May 19, 2015 Share Posted May 19, 2015 How are you generating the random number? What you need to do is run that code every time you want a different number. It should be an expression that involves Math.random(). Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now