Jump to content

Timers not working in different states


Recommended Posts

I've only been using Phaser for a few weeks now so this may be a noob issue, but here's the skinny:


My app is one that loads "pages" of interactive content in sequential order, with next/previous buttons at the bottom of the pages to take you to the next/previous page. A JSON file is read to get the list of assets to display on a given page, and also to control what events take place for the page and when they occur.


The game currently has three states, that are all defined in the same JS file: "Preloader", "PagePlayer", and "PageSwitcher", with a custom callback to be executed when the page shuts down:


game.state.add('preloader', Preloader);game.state.add('pageplayer', PagePlayer);game.state.add('pageswitcher', PageSwitcher);game.state.onShutDownCallback = "shutdown";game.state.start('preloader');

After the Preloader state runs, it starts the PagePlayer state with the first page to display, which works perfectly fine, and any timed events that need to occur are set up with calls like:

tTimer = game.time.events.add(tDelayBefore, IBExecuteBehavior, this, pElementID, tBehaviorObj, tBehavior, tTargetElementID);

and they trigger when they're supposed to. (NOTE: The IBExecuteBehavior function is in the 'global' JS area and not inside any given state.)


When the user clicks the Next button, the 'shutdown' callback executes and stops all the currently running timers for the page (I'm managing the references to the timers in the IBActivitiesMgr object where pg is the current page number):

if (Object.keys(IBActivitiesMgr[pg]['timers']).length > 0) {  for (t = 0; t<Object.keys(IBActivitiesMgr[pg]['timers']).length; t++) {     console.log("Stopping timer:" + t);     tTimer = IBActivitiesMgr[pg]['timers'][t];     tTimer.destroy();  }}

The console is showing that any timers for the page are destroyed properly, and then it switches to the PageSwitcher state (which does a few things) and then PageSwitcher starts PagePlayer again but with a different value for pg (so the next page's content is acquired and displayed). 


All that works fine - but here's the problem I'm having: any timers that get created by the PagePlayer state for any page other than the first one that runs will not trigger at the designated time - the game.time.events.add statement executes properly without error, but the IBExecuteBehavior function is never triggered - it's like the timer is "swallowed' or that it's trying to execute the function, but in a different context.


I know there have been reports about Timer issues - but can you see anything that I did wrong or that will fix this problem?




Link to comment
Share on other sites

Ruling out scope issues (i.e. the context has changed on the timer and it can no longer bind itself the way it expects), this could be related to some of the issues Timers used to have. On the plus side, they've been fixed and heavily tested in the 1.2 branch, which will go live on Thursday if you can hold on a couple more days.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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