[SOLVED] Reset score after lose/win condition

my lose condition is if hearts == 0; (executes score.js) and my win condition is if my player collides with the pillar at the end of each level (executes endFinish.js). any time after a win or lose is executed, the hearts and score still stay the number they were when the conditions were met.
eg. if your hearts == 0; and you lose, it would still be zero even after you replay instead of reset back up to 3. the same goes for score (resetting to zero score, that is).

is there a way i can make it so these counters reset after ever win/lose? this is the very last issue i’m facing and it’s really been bugging me. i’ve tried resetting score back to zero and hearts += 3 after the win/lose scene is triggered, but it seems like that’s not working. or maybe i’m doing something wrong. any help is much appreciated here!

project link: PlayCanvas | HTML5 Game Engine

Hi @GAB_VILLAPANDO!

Can you show how you did this?

hello @Albertos
it’s a very simple line that just adds or declares the number. i might be wrong on the execution but right on track with thought.
in the lose code (in score.js), it went something like this

Score.prototype.onCollisionStart = function (result) {
//if orbs are missed and hit the border
    if (result.other.tags.has('Orbs')) {
        //play sound and destroy
        this.entity.sound.play('Scratch');
        result.other.destroy();
        
        //then -1 health
        health -= 1;
        var hearts = this.app.root.findByName('HeartDisplay');
        hearts.element.text = health.toString();
       //lose condition
        if (health == 0) {
        //end game
        this.app.scenes.loadSceneHierarchy(this.app.scenes.find("Lose"));

        var CurrentScene = this.app.root.findByName("Root");
        CurrentScene.destroy();
        health += 3; //<--- add 3 health after end screen
    }
    }
}

and in the win code (in endFinish.js), this

//if pillar is hit, switch to win scene
EndFinish.prototype.onCollisionStart = function (result) {
    this.app.scenes.loadSceneHierarchy(this.app.scenes.find("Win"));

    var CurrentScene = this.app.root.findByName("Root");
    CurrentScene.destroy();
    score = 0; //<---- declare score zero again
};

after a coffee break and some tinkering around, i thought to declare these reset counters when the level starts instead of when the level ends. (also that i was using score = 0; instead of counter = 0; because score was the var for my score display, rather than the incrementing number itself!)

so when a level is started,

SongOne.prototype.initialize = function() {
    // selecting the Level 1 button
    this.entity.button.on('click', function(event) {
        // resets all counters
        health = 3;
        counter = 0;
        //loads the scene
        this.app.scenes.loadSceneHierarchy(this.app.scenes.find("Level 1"));

        var CurrentScene = this.app.root.findByName("Root");
        CurrentScene.destroy();
    }, this);
    
};

it works!! :laughing: i was indeed on the right track, just needed a new perspective on it.

1 Like