Kryllster Posted January 15, 2011 Share Posted January 15, 2011 I am working on a turn based game and I am having some difficulty making the HP counter stop counting at the specified number here is some of the code; <?php session_start(); $player_hp = $_SESSION['player_hp']; $turns = $_SESSION['turns']; $turns = $turns - 1; ?> and this is how I was trying to stop it. <?php if($total_hp < 30){ $player = $player_hp + 5; echo "You Still Have More recovery to do!"; } else { echo "You are fully recovered!"; } $_SESSION['turns'] = $turns; $_SESSION['player_hp'] = $player_hp; ?> I know this is simple but I'm having a heck of a time getting the HP to stop counting at 30. Also here is the code which I use to refresh the page to count it out. <form action="safe-spot.php" method="post"> <input type="hidden" name="sub"> <input type="submit" value="Click Here"> Thanks in advance, Quote Link to comment https://forums.phpfreaks.com/topic/224556-hello-world-2/ Share on other sites More sharing options...
Kryllster Posted January 15, 2011 Author Share Posted January 15, 2011 Sorry posted in the wrong forums!! Please forgive the intrusion! Quote Link to comment https://forums.phpfreaks.com/topic/224556-hello-world-2/#findComment-1159956 Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2011 Share Posted January 15, 2011 No problem. Moved it for you, and marked it unsolved again. Quote Link to comment https://forums.phpfreaks.com/topic/224556-hello-world-2/#findComment-1159959 Share on other sites More sharing options...
mckgr Posted January 15, 2011 Share Posted January 15, 2011 Can you go into a little more detail please? Is your problem that it is sometimes going above 30? If so, is it going above 34? What are the $total_hp and $player variables supposed to represent? I would hazard a guess that this is the problem. This should work better, and will make the upper limit for HP hard: <?php session_start(); $_SESSION['turns']--; if($_SESSION['player_hp'] < 30){ $_SESSION['player_hp'] += 5; echo "You Still Have More recovery to do!"; } else { if($_SESSION['player_hp'] > 30) $_SESSION['player_hp'] = 30; echo "You are fully recovered!"; } ?> I've cut out the middle man here, as well. It now stores directly into the session. Quote Link to comment https://forums.phpfreaks.com/topic/224556-hello-world-2/#findComment-1159981 Share on other sites More sharing options...
DavidAM Posted January 15, 2011 Share Posted January 15, 2011 Just for reference, it seems you are not using the same variable for counting and for testing: if($total_hp < 30){ $player = $player_hp + 5; None of your code snippets defined $total_hp, they all referred to $player_hp Quote Link to comment https://forums.phpfreaks.com/topic/224556-hello-world-2/#findComment-1159996 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.