scriptkid

Pause Button

Recommended Posts

Depends how you've coded your timer, show us that code for a better answer.

Another way that is a better general approach (this approach scales better and is more flexible) is to simply apply/remove (subscribe/unsubcribe) functions to a slave tick emitter in response to events that control whether your game is paused or running, and it just so happens you have a very handy slave right there in the browser with no work, requestAnimationFrame.

var tick = new EventEmitter()

requestAnimationFrame(function ticker () {
  tick.emit('tick')
  requestAnimationFrame(ticker)
})

function update () {
  ..stuff
}

function onGo () {
  tick.on('tick', update)
  pauseButton.removeEventListener(onGo)
  pauseButton.addEventListener(onPause)
}

function onPause () {
  tick.off('tick', update)
  pauseButton.addEventListener(onGo)
  pauseButton.removeEventListener(onPause)
}

 This way you could set up multiple timers, or limit some timers, or set up an observable slaved to the tick emitter for more control, etc etc etc.

Share this post


Link to post
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.

Guest
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.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.