Jump to content

How to call a function inside another function


Ced
 Share

Recommended Posts

Hello, So I actually have the following code that works:
 

var player;
var box_tnt;

function create (){
    this.physics.add.collider(player, box_tnt, hitTnt, null, this);
}

//the function hitTnt stop the game because the player died
function hitTnt (player, boxes){
    this.physics.pause();
    player.setTint(0xff0000);
    player.anims.play('default');
    gameOver = true;
}

and I want to do something like:

var player;
var box_tnt;

function create (){
    this.physics.add.collider(player, box_tnt, hitTnt, null, this);
}

//the function hitTnt stop the game because the player died
function hitTnt (player, boxes){
    gameOver();
    //other stuff here
}

function gameOver (){
    this.physics.pause();
    console.log('Game Over!');
    textGameOver.setText('GAME OVER');
    player.setTint(0xff0000);
    player.anims.play('default');
    gameOver = true;
}


but I have the following message:
 

Quote

TypeError: this.gameOver is not a function

Do you have please any ideas how to do it properly?

Link to comment
Share on other sites

The code above first assigns `gameOver` to a function, then the function reassigns itself to a boolean - `gameOver = true;`. Functions in JavaScript behave like any other variable, so that line overwrites the value of your function. You should change the name of one of them - either the function or your intended variable.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...