Embarissed Posted January 31, 2017 Share Posted January 31, 2017 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, https://github.com/embarissed/godawggaming/blob/master/js/entities/HUD.js 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. Quote Link to comment Share on other sites More sharing options...
obiot Posted February 1, 2017 Share Posted February 1, 2017 what I would probably do is to register on the `LevelLoaded` event and reset/start the tween when the callback is triggered. http://melonjs.github.io/melonJS/docs/me.event.html#LEVEL_LOADED Quote Link to comment Share on other sites More sharing options...
Embarissed Posted February 2, 2017 Author Share Posted February 2, 2017 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. Quote Link to comment Share on other sites More sharing options...
obiot Posted February 2, 2017 Share Posted February 2, 2017 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) Embarissed 1 Quote Link to comment Share on other sites More sharing options...
Embarissed Posted February 3, 2017 Author Share Posted February 3, 2017 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. Quote Link to comment Share on other sites More sharing options...
obiot Posted February 3, 2017 Share Posted February 3, 2017 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.audio.play("die"); me.state.change( me.state.GAMEOVER ); }); this.tween.isPersistent = true; this.tween.start(); 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 Quote Link to comment Share on other sites More sharing options...
obiot Posted February 3, 2017 Share Posted February 3, 2017 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 Embarissed 1 Quote Link to comment Share on other sites More sharing options...
Embarissed Posted February 3, 2017 Author Share Posted February 3, 2017 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 this.tween.stop(); } and that seems to work fine, but I appreciate the knowledge about level loaded in case I need it later. Quote Link to comment Share on other sites More sharing options...
obiot Posted February 4, 2017 Share Posted February 4, 2017 great ! and sure `onDestroyEvent` works too ! maybe I was overcomplicating things by using those events, but it gives more toys to play with in the future if you want :):):) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.