0

So I tracked my problem down to the function chargeBmAttk() not working so I added an alert to create an easier indication for debugging; However the alert will not pop up. Is there something wrong with the function chargeBmAttk() 's syntax or the button in the HTML code?

var playerHp = 45; // Player's health
var enemyHp = 35; // Enemy's health
// Player attack options
function missleAttk()
{ 
var playerDmgs = [5, 10, 15]; // Array of possible damages
var playerRndDmg = playerDmgs[Math.floor(Math.random() * playerDmgs.length]; // Randomly select integer from array and apply as playerRndDmg
enemyHp = enemyHp - playerRndDmg;
}
function chargeBmAttk()
{
alert("hello world");
// enemyHp = enemyHp - 5;
}
function waveBmAttk()
{
enemyHp = enemyHp - 7;
}
function iceBmAttk()
{
enemeyHp = enemeyHp - 10;
}
// Enemy attack options
function enemyAttk()
{
var enemyDmgs = [10, 7, 5, 4]; // Array of possible enemy damages
var enemyrRndDmg = enemyDmgs[Math.floor(Math.random() * enemyDmgs.length]; // Randomly select integer from array and apply as enemyRndDmg
playerHp = playerHp - enemeyRndDmg;
}
// Change txt outputs
function changePlayerHp()
{
var oldHp = document.getElementById("disPlayerHp").innerHTML;
var newHp = "Samus" + playerHp + "/ 45";
document.getElementById("disPlayerHp").innerHTML = newHp;
}
function changeEnemyHp()
{
var oldHp = document.getElementById("disEnemyHp").innerHTML;
var newHp = "Metroid" + enemyHp + "/ 35";
document.getElementById("disEnemyHp").innerHTML = newHp;
}
// Game end functions
function win()
{
if (enemyHp <= 0){
alert ("You win!");
}
}
function lose()
{
if (playerHp <= 0){
alert ("You Lose!");
}
}
// Game flow
function flow()
{
changeEnemyHp();
enemyAttk();
changePlayerHp();
win();
lose();
}
</script>
</head>
<body>
<div class="actions">
</br>
 <input type="button" onclick = "chargeBmAttk()" value = "Charge Beam">
 </button>
</div> 
</body>
Shrujan Shetty
2,4323 gold badges28 silver badges44 bronze badges
asked Feb 25, 2013 at 6:35
2
  • 2
    what's </button> doing there ? Commented Feb 25, 2013 at 6:39
  • have you already checked the output of the developer console (web console) on firefox or google chrome? There should be some error message concerning javascript I suppose... Commented Feb 25, 2013 at 6:39

3 Answers 3

1
this one is correct.you missed two ')' and closing input tag.
<!DOCTYPE html>
<html>
<script>
var playerHp = 45; // Player's health
var enemyHp = 35; // Enemy's health
// Player attack options
function missleAttk()
{ 
var playerDmgs = [5, 10, 15]; // Array of possible damages
var playerRndDmg = playerDmgs[Math.floor(Math.random() * playerDmgs.length)]; // Randomly select integer from array and apply as playerRndDmg
enemyHp = enemyHp - playerRndDmg;
}
function chargeBmAttk()
{
alert("hello world");
// enemyHp = enemyHp - 5;
}
function waveBmAttk()
{
enemyHp = enemyHp - 7;
}
function iceBmAttk()
{
enemeyHp = enemeyHp - 10;
}
// Enemy attack options
function enemyAttk()
{
var enemyDmgs = [10, 7, 5, 4]; // Array of possible enemy damages
var enemyrRndDmg = enemyDmgs[Math.floor(Math.random() * enemyDmgs.length)]; // Randomly select integer from array and apply as enemyRndDmg
playerHp = playerHp - enemeyRndDmg;
}
// Change txt outputs
function changePlayerHp()
{
var oldHp = document.getElementById("disPlayerHp").innerHTML;
var newHp = "Samus" + playerHp + "/ 45";
document.getElementById("disPlayerHp").innerHTML = newHp;
}
function changeEnemyHp()
{
var oldHp = document.getElementById("disEnemyHp").innerHTML;
var newHp = "Metroid" + enemyHp + "/ 35";
document.getElementById("disEnemyHp").innerHTML = newHp;
}
// Game end functions
function win()
{
if (enemyHp <= 0){
alert ("You win!");
}
}
function lose()
{
if (playerHp <= 0){
alert ("You Lose!");
}
}
// Game flow
function flow()
{
changeEnemyHp();
enemyAttk();
changePlayerHp();
win();
lose();
}
</script>
</head>
<body>
<div class="actions">
 <input type="button" onclick = "chargeBmAttk();" value = "Charge Beam" />
</div> 
</body>
</html>
answered Feb 25, 2013 at 7:00
Sign up to request clarification or add additional context in comments.

Comments

0

var playerRndDmg = playerDmgs[Math.floor(Math.random() * playerDmgs.length)];

var enemyrRndDmg = enemyDmgs[Math.floor(Math.random() * enemyDmgs.length)];

there is missing brace ) in this 2 line , after correcting your function will be call and alert Hello World

answered Feb 25, 2013 at 6:43

Comments

0

The problem is in your enemyAttk() function and missleAttk() function.

function enemyAttk()
{
var enemyDmgs = [10, 7, 5, 4]; // Array of possible enemy damages
var enemyrRndDmg = enemyDmgs[Math.floor(Math.random() * enemyDmgs.length)];//bracket missing here
playerHp = playerHp - enemeyRndDmg;
}

There is a bracket missing in that function.And you need to close the input tag in your HTML code. And remove </button> tag from your code.

answered Feb 25, 2013 at 7:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.