Sign in to follow this  
Ced

How to call a function inside another function

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.