Question

I want to create an onscreen pause button in andengine

What I do now is add an sprite and when I touch it, I do engine.stop(), the problem with this, is that the engine doesn't handle more touchevents till I resume the game (now I use the menu button for this), so is there a way to achieve it?

Thanks!

Was it helpful?

Solution 2

I found the best way to do it is creating an scene, and when paused, set override the onManagedUpdate this way

@Override
onManagedUpdate(float pSecondsElapsed){
 if(mPaused) super.onManagedUpdate(0);
 else        super.onManagedUpdate(pSecondsElapsed);
}

This way everything works perfectly, and you can do it on a game layer and have the menu layer updated as usual,

OTHER TIPS

Look at the AndEngine's examples, there is a project that shows the use of menus in AndEngine, you'll find a better way of implementing a menu other than stopping the engine. Good luck!

you can do on screen pause button pretty easily. What you need to do is, create a pause button anywhere on screen, on touching that button, show a MenuScene with play button over the original pause button. You can use .setPosition() for MenuItem if you comment out the .buildAnimations() code from the PauseMenu example.

In order to demonstrate the idea, I've made a simple activity to show its possible. Take a look and try for yourself.

Link: https://github.com/reittes/On-Screen-Pause-Button

GoodLuck

What I would do is add a "paused" boolean in your file and then have the button set it to true, then encompass your engine updater with a if (!pause) {...} block to make it stop updating when it's paused. Not the most elegant solution but worked in my game and caused no performance issues on unpause.

Psedu code

@Override
public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
    if (pKeyCode == KeyEvent.KEYCODE_MENU && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
        if (this.mEngine.isRunning()) {
            gSceneGlobal.setChildScene(this.mGamePauseScene, false, true, true);
            this.mEngine.stop();
        } else {
            gSceneGlobal.clearChildScene();
            this.mEngine.start();
        }
        return true;
    } else {
        return super.onKeyDown(pKeyCode, pEvent);
    }
}

I create my class (MyEngine extends Engine) and changed

@Override
public boolean onTouch(final View pView, final MotionEvent pSurfaceMotionEvent) {                      
if(!isRunning()) {
  // add your code for engine.stop();
  }
}

and create MyEngine engine in game class;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top