We cannot create an onscreen pause button in andengine??

  ... discussions about development with the GLES1 branch of AndEngine.

We cannot create an onscreen pause button in andengine??

Postby onerain88 » Sat Feb 18, 2012 2:02 pm

Hi, guys.
Yesterday, when I design my game in AndEngine, I found I cannot create an onscreen pause button in my game! So frustrate!
I try to create a ButtonSprite as the pause button of my game! When the pause button click, I take mEngine.stop(). But when I click the pause button agagin, it cannot star...(Of course, I wrote a bool variable in order to control mEngine.start() or mEngine.stop()).
So, I read the source code of AndEngine, I found in mEngine.stop(), mRunning become false. In Engine.onTickUpdate(), if mRunning is false, it will not update, but in it's update, it will listen the touch event of the screen!
In AndEngineExamples, there is an example of pause, but it used Menu key instead of an onscreen button. It works, because Menu key is system listen, not engine listen....
So, I have a try to Engine.java. I add a bool variable called pause inited false. When I click the pause button on screen, the pause turn true, and I will judge the pause in Engine.update(), as follow,
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onUpdate(final long pNanosecondsElapsed) throws InterruptedException
  2.         {
  3.                 final float pSecondsElapsed = pNanosecondsElapsed * TimeConstants.SECONDS_PER_NANOSECOND;
  4.  
  5.                 this.mSecondsElapsedTotal += pSecondsElapsed;
  6.                 this.mLastTick += pNanosecondsElapsed;
  7.  
  8.                 this.mTouchController.onUpdate(pSecondsElapsed);
  9.                
  10.                 if (!pause)
  11.                 {
  12.                         this.onUpdateUpdateHandlers(pSecondsElapsed);
  13.                         this.onUpdateScene(pSecondsElapsed);
  14.                 }
  15.         }
  16.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

It means whatever the pause is true or false, it will listen the touch on screen!
onerain88
 
Posts: 8
Joined: Mon Feb 06, 2012 6:12 am

Re: We cannot create an onscreen pause button in andengine??

Postby reittes » Sat Feb 18, 2012 3:33 pm

Hi,

I had same problem a while back, but I think we could do a workaround. You can place an on screen pause button, when that button is clicked, show a MenuScene (from andengine menu example). If you comment out the .buildAnimations code from menuScene, I think you can place(using .setposition) the same pause button(now play button) over the actual pause button on the scene. I hope you get what I'm trying to explain, I haven't tried it though.

Goodluck :)
---------------------------------------------
My Apps: https://play.google.com/store/apps/deve ... eizon+Apps
reittes
 
Posts: 59
Joined: Tue May 24, 2011 4:56 pm

Re: We cannot create an onscreen pause button in andengine??

Postby reittes » Sat Feb 18, 2012 6:05 pm

Ok, I tried what I wanted to explain before, and it worked :)

Here, you can see the example of onscreen pause button, the project is uploaded on my github.

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

GoodLuck
---------------------------------------------
My Apps: https://play.google.com/store/apps/deve ... eizon+Apps
reittes
 
Posts: 59
Joined: Tue May 24, 2011 4:56 pm

Re: We cannot create an onscreen pause button in andengine??

Postby onerain88 » Sun Feb 19, 2012 3:33 am

Thanks reittes,
You provide a better solution for the problem! Thank you again!
After I read your code, I found I cannot understand the parameters of
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setChildScene(final Scene pChildScene, final boolean pModalDraw, final boolean pModalUpdate,
  2.                         final boolean pModalTouch)
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

In your code, it's
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. scene.setChildScene(pauseScene(), false, true, true);
  2.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

I couldnot understand why pModalUpdate is true.
onerain88
 
Posts: 8
Joined: Mon Feb 06, 2012 6:12 am

Re: We cannot create an onscreen pause button in andengine??

Postby reittes » Sun Feb 19, 2012 5:09 am

I don't know much about it but i think parameters just define the modal child/scene we show. A modal child stops the scene behind and shows the pause scene in our case with its own touch touch and update handlers. I don't know much, just rough idea, from the examples.
---------------------------------------------
My Apps: https://play.google.com/store/apps/deve ... eizon+Apps
reittes
 
Posts: 59
Joined: Tue May 24, 2011 4:56 pm

Re: We cannot create an onscreen pause button in andengine??

Postby onerain88 » Sun Feb 19, 2012 6:04 am

Thank you, reittes!
I will read the source code more patiently!
Good luck!
onerain88
 
Posts: 8
Joined: Mon Feb 06, 2012 6:12 am

Re: We cannot create an onscreen pause button in andengine??

Postby shihab_returns » Tue Oct 23, 2012 4:42 pm

hey onerain88, thanks for your post.

i cann't understand where i use onUpdate() method.

I follow you tutorial about onscreen pause button.

but in my code touch listener listen just once.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. pauseBtn = new Sprite(cameraWidth - 200, 50, btn_pauseRegion) {
  2.                         @Override
  3.                         public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
  4.                                         float pTouchAreaLocalX, float pTouchAreaLocalY) {
  5.                                 if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_UP) {
  6.                                         if (!pauseFlag) {
  7.                                                 this.setVisible(false);
  8.                                                 mMainScene.setChildScene(pauseMenuScene(), false, true,
  9.                                                                 true);
  10.                                                  mEngine.stop();
  11.                                         }
  12.  
  13.                                         else {
  14.                                                 mEngine.start();
  15.                                                 pauseBtn.setVisible(true);
  16.                                                 mMainScene.clearChildScene();
  17.                                                
  18.                                         }
  19.  
  20.                                 }
  21.  
  22.                                 return true;
  23.                         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
shihab_returns
 
Posts: 101
Joined: Tue Oct 16, 2012 8:48 am
Location: Dhaka

Re: We cannot create an onscreen pause button in andengine??

Postby korn3l » Sat Oct 27, 2012 7:52 pm

You should not use engine.stop() and engine.start() when you pause your game. Delete them and it should be fine.
When you set the child scene the parent will be paused if the modal parameters are set right;
Take a loo at the setChildScene method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void setChildScene(final Scene pChildScene, final boolean pModalDraw, final boolean pModalUpdate, final boolean pModalTouch);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

- pModalDraw - setting this to false will let the parent call its onDraw method and you want that because otherwise it will look nasty;
- pModalUpdate - if set to true the parent won't call its onUpdate method so things like position, rotation or other operations won't apply anymore. It basically pauses your game.
-pModalTouch - decides whether the parent receives the touch or not; if you set this to true the parent won't respond to touch actions
korn3l
 
Posts: 214
Joined: Thu Apr 12, 2012 4:20 pm
Location: Iasi, Romania


Return to GLES1

Who is online

Users browsing this forum: No registered users and 17 guests