As of late 2021, we are in the process of updating Stencylpedia and fixing up outdated & broken content. Thanks for your patience!

18,538

Views

Beginner

Level

3

Comments

Sending an App to the Background

by Jon (Updated on 2015-10-17)


Contents

  • Introduction
  • Use the Focus Event
  • Best Practice - Pause the Game
  • FAQ: Why does my app restart from scratch?

Introduction

Many modern mobile devices support some form of multitasking -- the ability to switch between several apps by sending the currently active app to the "background."

Stencyl provides a simple but effective way of performing logic when an app enters and leaves the background.

Use the Focus Event

The Focus event (under Add Event > Input) not only works on web/desktop games, but it also works the same way on mobile games.

Focus Event

Focus Event Block

The Focus event will happen in two cases.

1) Losing Focus

Happens just before the game is sent to the background.

2) Regaining Focus

Happens just after the game resumes but before anything else happens to it.

Best Practice - Pause the Game

When would you use the Focus event in a mobile game?

It’s a good idea to pause a game when it enters the background. When that happens, it's also a good idea to display some appropriate graphics to indicate the game's paused status.

Losing Focus

Paused

Why is this a good idea? Imagine that a player switched away from the app but was left in a situation where he'd get hit within seconds. If the app weren't paused, the player would likely be too slow to react and would get frustrated.

FAQ: Why does my app restart from scratch?

Multitasking on iOS or Android doesn't work the same way as it does on a PC or Mac. If the amount of free memory on the device is too little, the device will stop a backgrounded app to free up memory, so that it can support the task at hand. This causes the game to start from scratch - likely lost progress for the player.

If you wish to support true multitasking, we recommend that you save the game upon losing focus and offer to load the saved game if you detect that the game has restarted (similar to the "Quick Save" option in recent Mario games).

Print Article Edit Article How to Edit an Article
Disclaimer: Use comments to provide feedback and point out issues with the article (typo, wrong info, etc.). If you're seeking help for your game, please ask a question on the forums. Thanks!

3 Comments

Chris24XD
So:
Losing Focus = Pause
Gaining Focus = Unpause
Pretty useful (l guess)

0 4 years, 2 months ago
Jon
Updated the article and yeah, it was backwards.
0 8 years, 11 months ago
TwistedIdentity
@ Jon... Don't you have this backwards??

1) Gaining Focus

Fired just before an app is about to go to the background.

2) Losing Focus

Fired just after an app is resumed but before anything else happens to it.

3 10 years, 8 months ago

Sign In to Comment