2
\$\begingroup\$

I'd like to embed HTML5 games in a website, but I need to be able to detect when the game has ended so I can run some of my own JavaScript.

Looking at the output of some game engines (Construct 2, for example) reveals a nightmare of code that I couldn't hope to tweak so that it fires off an event when done.

How can I detect when a game has ended?

\$\endgroup\$
  • \$\begingroup\$ If this is just some 3rd party game, you can pick out the code and inject a global callback and just listen for that. Dissecting the code is out of scope for this stack exchange. \$\endgroup\$ – Vaughan Hilts Apr 24 '14 at 0:45
  • \$\begingroup\$ Embedding 3rd party javascript code into your website is generally a bad idea, because it is almost impossible to adequately sandbox it against messing with your own parts of the website. \$\endgroup\$ – Philipp Apr 24 '14 at 9:42
4
\$\begingroup\$

Unless the game or framework offers some sort of API that let's the webpage check on its state, there no easy way to do this. Technically, the game is always "running" even if the player is just sitting in the menu, which is why you would need the game to tell your webpage when the player has completed the level.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

I don't see why this is necessarily that difficult to approach. It depends what triggers a "Game Over," but take for example some 2D collision based game, where you loose when you fall out of bounds. You'd have a simple isInBounds() function or something that you could check when running the main which might lead to some gameOver() function when to is false. From there you could reference any of your own javascript you want to.

A really good example of all of this can be found here. It gives a broader tutorial on creating a whole game, but it has a section that deals specifically with a 'gameOver()` event.

If this doesn't answer your question, please tell me.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.