Jump to content



Recommended Posts

First off, thanks for all the work and effort the commenters do to help us little guys out.

I have a timer in my platformer game, with help from The Uncertainty Principle by Radmars, in my HUD.js with the following code, 


The life and score work correctly across multiple levels. The timer works on the first level (including changing to game over screen,) but freezes when the second level is loaded. I've been stumped by this for a few days so I figured I'd ask for help. Thank you.

Link to comment
Share on other sites

I should have mentioned in the original post, I actually want the timer to continue across all levels as I will have many short levels and a longer timer. If I understand what you are saying, that would reset the clock after each level? I think I am having trouble understanding why life and score keep updating yet the timer stops.

Link to comment
Share on other sites

Oh I see it now, the tween itself being added to the world container upon creation, it gets removed when you load a new level.

setting it persistent as well should solve your issue, but it also means that the timer will take in account the level changing/loading time. To solve that (if bothering you) you can also pause the tween when changing and then use the level loaded event to resume if once the new one is loaded (or just save the timer value, and create a new tween if you do not want to make the tween persistent)

Link to comment
Share on other sites

Just for clarification, where is the level loaded event? I didn't think I had one, only a resetEvent when loading the game in play.js And I'm ashamed to say it but I'm not sure how to add persistence specifically to the tween. I have it added to the HUD container with the clock added as a child but obviously that isn't working.

Again, thanks for taking time out of your day to help.

Link to comment
Share on other sites

for the persistent flag, you just need add the flag to your tween


this.tween = new me.Tween(this).to({
		remainingTime: 0,
	}, this.remainingTime * 1000).onComplete(() => {
		//throw "GG BRO";
		me.state.change( me.state.GAMEOVER );				

this.tween.isPersistent = true;



for the level_loaded, it's about subscribing to the event, see the doc link i put before, but I think that the persistent flag here should be enough to fix your issue

Link to comment
Share on other sites

to clarify the part on the event : there is no "predefined" level loaded callback in a screen object.

game.HUD.DeathClock = me.Renderable.extend({
    init: function(x, y) {
    // .. your code
    // subscribe to the level loaded event
    me.event.subscribe(me.event.LEVEL_LOADED, this.levelLoaded.bind(this));

// callback for when a level is loaded
levelLoaded: function(levelName) {
   // reset or start the tween

couple of additional comments :

- you probably also need to remove or pause the tween manually when you for example switch back to the title screen (if you have one)

- you will also need to unsubscribe to this event when also switching back to the title screen or any other not "in-game" part, see http://melonjs.github.io/melonJS/docs/me.event.html#unsubscribe

Link to comment
Share on other sites

Thank you so much! First part worked like a charm, and just like you said, it continued through to the title screen. I just added, 

	onDestroyEvent : function () {
        //just in case

and that seems to work fine, but I appreciate the knowledge about level loaded in case I need it later.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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