Home > Enterprise >  My HTML button is not working, or it wont call my javascript function
My HTML button is not working, or it wont call my javascript function

Time:01-29

I am new to coding, and i am trying to make a game in javascript. I don't know why my HTML button is not calling my javascript function. I have tried moving the button to the bottom of the code, I have tried it with different functions, I have looked on some reference sites and i can't figure out why it is not working. Here is my button:

let waveActive = false;
let waveNumber = 0;
let enemyCount = 4;
let enemyAmount = 0;
let enemyHealth = 30;

function newWave() {
  enemyCount   25 %
    Math.floor(enemyCount);
  enemyHealth   10 %
    Math.floor(enemyHealth);
}

function startWave() {
  waveNumber   1;
  waveActive = true;
  newWave()
  class enemySpawn {
    main(Stringargs) {
      int(i = enemyAmount)
      while (i < enemyCount) {
        Enemy = new component(30, 30, "purple", 10, 120);
        enemyAmount   1;
      }
    }
    enemyAmount = 0;
  }
}
<div name="buttonDiv">
  <button type="button" id="startRoundBtn" onclick=startWave();>Start Round</button>
</div>

CodePudding user response:

Try to increment your wave number. waveNumber 1 will not change the property.

waveNumber  = 1;

Here too (along with other places):

function newWave() {
  enemyCount = enemyCount   25 %
    Math.floor(enemyCount);
  enemyHealth = enemyHealth   10 %
    Math.floor(enemyHealth);
}

The other issue is your class definition inside your click handler.

You probably do not need the main function. It seems you want something more like this

class EnemySpawn {
  constructor(...args) {
     // do stuff here
  }
}

enemyAmount = 0;  // not sure if you even want to reset this...

while (enemyAmount < enemyCount) {
 const enemy = new EnemySpawn(30, 30, "purple", 10, 120);
 enemyAmount  = 1;
}

CodePudding user response:

Try it in Script tag in html it should work. Below code is working for me :)

<div name="buttonDiv">
  <button type="button" id="startRoundBtn" onclick="startWave();">
    Start Round
  </button>
</div>

<script>
    let waveActive = false;
let waveNumber = 0;
let enemyCount = 4;
let enemyAmount = 0;
let enemyHealth = 30;
function newWave() {
enemyCount   25%
Math.floor(enemyCount);
enemyHealth   10%
Math.floor(enemyHealth);
}
function startWave() {
  waveNumber   1;
  waveActive = true;
  newWave()
  class enemySpawn {
     main(Stringargs) {
        int(i = enemyAmount)
        while (i < enemyCount) {
            Enemy = new component(30, 30, "purple", 10, 120);
            enemyAmount   1;
       }
    }
    enemyAmount = 0;
  }
}
</script>
  •  Tags:  
  • Related