[SOLVED] Timer runs out back to main menu

Hi so here again to ask for help sorry for this but what code do i add if i want to make the game lose when the timer runs out?

in which part should i add if this timer == 0?

here is my current code set for timer

var Timer = pc.createScript('timer');

Timer.attributes.add('textElement', {type: 'entity'});

//correct one

// initialize code called once per entity

Timer.prototype.initialize = function() {

    this.timer = 180;

    this.updateText();

};

// update code called every frame

Timer.prototype.update = function(dt) {

    this.timer -= dt;  

    this.updateText();

};

Timer.prototype.updateText = function() {

    var roundedTimer = Math.floor(this.timer);

    var minutes = Math.floor(roundedTimer / 60);

    var seconds = Math.floor(roundedTimer % 60);

    this.textElement.element.text = minutes + ":" + seconds;

};

You can do this in the update function of your script.

image

like this?

i did this but i think now it goes directly to the defeat page instead of doing the count down how do i fix that?

var Timer = pc.createScript('timer');


Timer.attributes.add('textElement', {type: 'entity'});

//correct one
// initialize code called once per entity
Timer.prototype.initialize = function() {
    this.timer = 180;
    this.updateText();
};

// update code called every frame
Timer.prototype.update = function(dt) {
    this.timer -= dt;   
    this.updateText();
    
    if(this.timer == 0); {
        var SceneToOpen = this.app.getSceneUrl('Defeat Page');
        this.app.scenes.loadScene(SceneToOpen);
        this.timer += 180;
        
        var CurrentScene = this.app.root.findByName('Root');
            CurrentScene.destroy();

        }
};

Timer.prototype.updateText = function() {
    var roundedTimer = Math.floor(this.timer);
    var minutes = Math.floor(roundedTimer / 60);
    var seconds = Math.floor(roundedTimer % 60);

    this.textElement.element.text = minutes + ":" + seconds;
};


So what is happening here is when the timer is equal to 0 you create the Defeat Page Load it, and then directly destroy the current scene. Also, The +=180, are you wanting to reset the timer to 180 again. I think there is just some misplaced items here and will look further.

So to clarify what operation you are looking for… Your game starts and runs for 180 frame counts. Once this happens you want to display a Defeat Page. How long do you want the Defeat Page to be displayed? Do you want to wait a bit and then go back to the main scene. Please describe your logic so we can help.

so i have this game and the timer is for 3:00 in text which is 180s, as written in the code. What i want is after 3mins the game will show the defeat page with the home button.

the += 180 is i think i need it once i choose to play again or that code isn’t necessary?

On line 18 there is an incorrect ; sign.

1 Like

fixed it but my timer still goes negative -1:-1 instead of going to the next scene as it reach 0:0

That’s because your timer is most likely never exactly 0. Instead of using ==, I will suggest to use <=.

2 Likes

Ohh ok ill give that a try

It worked, thank you so much!!!

1 Like