In my game when I press menu key and then back to the game it continues from where it paused
e.g. if I have a sprite moving to 0, 0 from full screen width and height and stopped at width/2 and height/2 it will start from this point and so on other things like music which stops then continues although I do nothing in pause and resume
My question is what should I do inside pause() and resume() of screens or main game class or should I leave them empty as they are ?
In main game class pause() and resume() I call super.pause() and super.resume()
I am loading my assets in show() should I do thing with this ?
When you press the home button, your game is not rendering until you open the game again. So your sprite moves on (is beeing rendered) at the same position because the reder() method was not beeing called while you left the game, thus not changing the coordinates of your sprite(s).
The pause() method is called on Android when the Home button is pressed or an incoming call is received. You could leave this method empty or for example save your game status to file.
The resume() method is called when the application resumed from pause state, that means if you open it again. You could also leave this method empty or for example show an options menu, so that the user knows that the game paused while he went out of the game.
For further information on the life cycle and states of a libgdx application, check the official documentation for this.
You could load your assets at the application start up (maybe you should use an AssetManager) in the main class (which extends Game) and pass a reference of the assets/AssetManager from the main class to every new Screen class you instantiate. This link in the libgdx wiki explains the act of managing assets.
Related
In my android game there is an arcade mode which runs for 60 sec. The gamescreen consists of a gameboard, which consists of a 6x6 matrix of coloured circles drawn on a surfaceview. There is a timer and scoreboard to keep track of time and score. Timer is basically a separate thread sleeping for 60 sec and updating a handler attached to UI thread every sec. As soon as time left becomes zero the games goes to another activity where the player's current score and past scores are displayed. If the player presses back key then previous activity (gamescreen) becomes visible, however the scoreboard is not reset but the matrix is redrawn. All the coding is in onCreate() method or new methods created for the game. There is no code in onPause() or onResume() methods. Then why the surfaceview is recreated and redrawn ? I dont think pressing back key runs onCreate() method.
Your first activity will be destroyed if you call explicitly call finish().
Even if you do not, your activity can be destroyed any time after it becomes fully obscured.
So it is wrong to presume onCreate() will not be called.
On cocos2d-x's game. When I pressed the home button to quit the game. The next time, how can I make the game must start over again, but not followed by the last exit scene
Ideally in applicationWillEnterForeground you should simply replace the scene with a new instance of it to start over e.g. if the class of scene you were running is called GameLevelScene you should simply tell CCDirector to replace the running scene with a new instance from it's class. For example:
CCDirector::sharedDirector()->replaceScene(GameLevelScene::create());
But while I was doing something similar to pause my game on resuming from background I noticed that it wasn't quite working (it should've, maybe this is some problem with cocos2d-x). So, as a work around, I created a sequence with a delay time of zero at it's start and then called my game's pause function. I guess the game needed to take one tick to process this after resuming from background. For you, if the above doesn't work just try this:
someNode->runAction(
CCSequence::create(CCDelayTime::create(0.0f),
CCCallFuncO::create(CCDirector::sharedDirector(),
callfuncO_selector(CCDirector::replaceScene),
GameLevelScene::create()),
NULL));
someNode can be any node in your game which is living in the game i.e. it hasn't been destroyed; it must be an alive object. You can have a game manager node which is alive at all times during your game. The game manager node can be made responsible for such game management issues.
I am experiencing a very strange phenomenon (test device: HTC Desire HD, Android 2.3.5). I know that System.gc() is needless and discouraged, and I don't try to suggest otherwise, but the point is that it shouldn't cause issues either (i.e. it should be useless at most).
I have an application which contains a GLSurfaceView in its view hierarchy. The GLSurfaceView is instantiated and added in the Activity.onCreate(). Normally, the application works like this:
User starts the app and goes to mainmenu
User chooses a mainmenu item which sets the GLSurfaceView to View.VISIBLE
User plays with the in-built game on GLSurfaceView
User goes to mainmenu and exits the activity (=> Activity.finish() is called)
My Activity.onPause() looks like this:
mGameThread.pause(); // gameThread is my custom thread class for the in-built game
mGLView.onPause(); // pause the renderer thread
So far so good, everything works fine. However, issues appear after I add the following code to onPause() (for the case when the user exits the game from the mainmenu):
mGameThread.pause(); // gameThread is my custom thread class for the in-built game
mGLView.onPause(); // pause the renderer thread
if (isFinishing()) {
System.gc();
}
In details: if the Activity is started for the first time (= i.e. the app process didn't exist before), everything works fine. However, starting from the 2nd start of the activity (= after the first exit from the mainmenu, i.e. after the first Activity.finish()), the framerate of GLSurfaceView is reduced by 40-50%, the in-built game becomes slow.
If I remove the System.gc() call, the problem disappears. Moreover, if I do the following, it also gets rid of the problem:
mGameThread.pause(); // gameThread is my custom thread class for the in-built game
mGLView.onPause(); // pause the renderer thread
if (isFinishing()) {
// 1. get layout root of View hierarchy
// 2. recursively remove (detach) all Views
// 3. call GC
System.gc();
}
I didn't add concrete code because it's complex, so I used comments. If I just detach the GLSurfaceView via removeView(), it is not enough. The entire view hierarchy needs to be cleared.
Note that I couldn't find any memory leaks (no Activity leak via drawables/statics etc.). Moreover, of course, the gameThread properly exits when the app is closed (I just didn't include its source code).
Any ideas, guesses? Apparently, System.gc() seems to cause some issues for the Activity/layout destroying mechanism of Android. Again, as I said, if I remove System.gc(), the problem disappears.
I have experience of Android Game Programming. I used to clear all the view in hierarchy because when running threads if you call System.gc() sometimes it happens that your thread has a reference to some of your view, even if you call system.gc() this view won't get removed and if you keep playing again and again this game you will notice that your heap memory is started growing.
It depends upon the memory leak, if you are leaking some KB memory it will take more time to crash your game. The best way it to use Eclipse Memory Anlyser (Eclipse MAT) and compare your stacks.
Step1:
take memory snap shot when you start your game for first time
Step2:
take memory snap shot when you start your game second time
Step3:
Now compare your both stacks of snapshots it will tell you the difference.
It is a very useful tool. I was having huge memory issues in my game Apache Attack. I fixed them using this awesome tool.
Follow this ECLIPSE MAT TUTORIAL
I have an Activity that plays some audio from within a ListActivity. The user can also click on the list item and go to a more detailed view of that data. I want the audio to keep playing during that transition from one activity to another and keep playing once the detail activity is active BUT I also need to pause the audio if my activity is being paused or stopped for any other reason.
My issue is that I currently pause the audio in onPause() in the ListActivity because I want the audio to be paused when the user navigates away from my activity. E.g. when they press Home or Back. However, I don't want the audio to pause when my detailed view activity gets started. onPause() is called in both instances, so how can I distinguish between the two cases?
Shouldn't you be doing that in a service instead? Wouldn't it be easier? Then you can stop the service when one or another activity is pausing or exiting (depending on which one you want). You could also check if it's started, and stop accordingly whenever you want.
Or sorry if I didn't understand your question. I see you have a lot of points here on SO, so perhaps I'm just confused.
--- edited since your third comment:
A Intent to B: send Intent with name of music.
B onCreate: get name of music from Intent. Set flag x to true.
B onResume: start playing music if from A or resumes from last known position if resuming from B.
B back button: override and set x to false. Actually, you'd cover all points where your activity finishes.
B onPause: stop the music if(x), store last known position of music in memory and stop service.
Here I assume that you want music to keep playing even when returning to A (that's what you said, that's the problem), not just up to B. Setting x is important early on, IMO, because if any other activity (phone call, anything) appears, activity will stop playing the music immediately (user expects that) on onPause. According to Android guidelines, you know that you're getting back to A only if user presses back button or if you finish() your activity. You can fine tuning checking the position of activities in your task (don't remember how to do that right now).
Personally, I also wouldn't want to resume playback on A (say, B->call->home->A, applying step 3 to A, too), because a disruption in the logical flow of things happens there.
CAVEAT: I would make sure that there is no other way. I would try to see if you can 1) know in advance which activity is about to be displayed. 2) get any music service to hook up to your task (is that even possible?).
Anyway, just use my solution if you can't find a clever way to do that. That would be my suggestion. Good luck.
I have Two activities One splash screen, Player screen.
When user clicks on my app icon first splash screen is displayed and then player screen
When player activity is running, if user returns to the home screen
and then again clicks on app icon, the application is starting from the splash screen again.
can any one please help me out how to do any one of below
1) I need to close current running activity and reload application.
or
2) I need to resume to the player screen directly.
Please give me an example or reference to follow, Im beginner in android programing
Thanks In advance
You may want to overrive the onRestart() method - it will be invoked if your Activity has been stopped previously; while it won't if it's the first time it runs (or if it has effectively been killed in the meantime).
Be sure to read the Activity life cycle there: http://developer.android.com/guide/topics/fundamentals.html#lcycles to understand what happens when.
Check out the order in which the life cycle states appear. You could override onStart, onPause, onResume, onStop, onCreate, onDestroy etc functions by adding Toasts to see the sequence. Then you can override them as per your programming requirements.