How to pause a game in cocos2d android? I searched many tutorials but i did not found anywhere.I tried below code, it is working when paused from game scene.But it not working while resuming the game from pause scene
in gameScene :
CCDirector.sharedDirector().onPause();
in pause Scene :
CCDirector.sharedDirector().onResume();
For pausing game scene I use:
CCDirector.sharedDirector().pause();
And for resuming:
CCDirector.sharedDirector().resume();
Try it with these functions. :)
Thanks for your reply. I used the same thing.But it wont stop schedulers and actions.
But finally i got the way we have to pause.
1.To pause we have to use pushScene() method as follows:
CCScene pauseScene = CCScene.node();
pauseScene.addChild(new PauseLayer);
CCDirector.sharedDirector().pushScene(pauseScene);
When you have done this, gamescene will get stored in the stack and now running scene will be pause scene.
2.To resume we have to use popScene() as follows:
CCDirector.sharedDirector().popScene();
3.If you are moving to LevelSelection form pause then along with above lines use these lines also:
CCDirector.sharedDirector().getRunningScene().removeAllChildren(true);
CCDirector.sharedDirector().replaceScene(levelSelectScene);
Declare a BOOL variable, then when you detect user pauses game, make this variable to NO and in your update method put this as a condition above to the code you want to pause on your game paused.
-(void)update:(ccTime)dt
{
if(isGameNotPaused)
{
// your code
}
}
Related
I'm creating a game using Cocos2d-x. I'm currently creating a gameover menu, in that menu I need to be able to switch to both my menuscene and my gamescene (When I say switch to gamescene I only really only mean "to restart" the game). But circular dependencies stop me from being able to do this.
MenuScene needs to be able to useGameScene::create() in order to switch to the gamescene and the gameover menu needs to be able to use both of GameScene::create() or its restart funtion and MenuScene::create() which is giving me circular dependency problems
I can't separate my gameover menu to it's own file as I still need the GameScene dependency and GameScene would need gameover.
I can't combine them as GameScene then needs to depend on MenuScene
So my question is: How do I alternate between two scenes in cocos2d-x c++.
I read somewhere about pushing and popnig scenes in Director, but I don't really understand how that works, or if I could use that for my purpose.
Thank you in advance!
EDIT:
Now that I think about it, could I not just push mMenuScene to Director before switching to GameScene? That should work if I understand that push/pop mechanic correctly.
I think you might have a misconception of how complex this is, using the way I provided below you can and should definitely split your game over scene into its own file.
The scene replace is easy enough, just use the code below:
Including your file:
#include "MainGameScene.h"
Creating and switching scenes in your onClickListener:
auto gameOverScene = GameOverScene::createScene();
// use code below for hard replace
Director::getInstance()->replaceScene(gameOverScene );
// or use code below for transition fade replace
Director::getInstance()->replaceScene(TransitionFade::create(1, gameOverScene , Color3B(255, 255, 255)));
As for the restart functionality. I usually provide a callback to my game over scene that I call when the restart button has been clicked. Not that I ever swap out my scene completely for a mobile game over scene, but I still do it the same way regardless. So lets do steps (This assumes you seperated your game over scene into it's own file named GameOverScene :) ).
Store a function pointer in your GameOverScene.h to your reset method in MainGameScene:
std::function<void()> _resetCallback;
Set your function pointer from the main game scene, before running with the GameOverScene.
auto gameOverScene = GameOverScene::createGameOverScene();
gameOverScene->setResetCallback(std::bind(&MainGameScene::reset, this));
When your reset button is clicked, call the _resetCallback
void GameOverScene::onResetClicked(Ref* sender)
{
_resetCallback();
}
This should provide you with all the functionality you need to set up a what you want as well as remove the circular dependency that you have. I have used this way many times before and it always works. Let me know if this solution works for you.
I am having a application in cocos2d-x. I want my application should run always until manually stops. But while enters into the background it got paused and when return back it resumes. I removed the codes
CCDirector::sharedDirector()->pause();
CCDirector::sharedDirector()->resume();
from Appdelegate.cpp functions (applicationDidEnterBackground() and applicationDidEnterForeground()). But still my application got pause while enter into background.I want my application should pause only when i got a phone call or manually i pause my application, until or unless it should run in the background. Please me to solve this.
Here is what happens when your app goes to background
class name >>> Cocos2dxActivity.java
#Override
protected void onPause() {
super.onPause();
Cocos2dxHelper.onPause();
this.mGLSurfaceView.onPause();
}
I think the code is self explanatory,i.e, it is pausing your app when it enters background (the whole purpose of onPause) and the ApplicationDelegate methods you mentioned are just JNI functions called when the activity goes to background/foreground so that you can do game related stuff there. As you may know that CCDirector's pause only pauses the running scene (scheduled timers) and changes the draw rate to 4 FPS to reduce CPU consumption.
So, removing pause/resume calls to CCDirector in Appdelegate will not help achieve those goals. For that you have to override the method and related methods I mentioned above and stop the activity from pausing.
My program takes a few seconds to start up. I am using clutter for the GUI, and I decided to try and make something pop up to indicate that the program is starting up. I wanted to just have a logo pop up and rotate, then disappear when the program starts.
So in clutter, I figured I could just make a new stage (window) add an actor to it, make the and actor spin, in the first section of the main function. The window will pop up right away, but with no content, but the content wont show until you launch the clutter main loop.
So I was just wondering how I might be able to achieve this using clutter or GTK+.
If you are familar with reaper 4, the audio recording program, this program does something similar to what I want to mine to do.
What you want is called a splash screen. I'm unfamiliar with clutter, but I found this GTK splash screen example.
However, I think you're taking the problem the wrong way. Splash screens are a bad idea because you just add overhead. What you need is improving your startup performance, by doing some CPU and/or IO profiling. Loading stuff on-demand, and not all at once will help.
Unfortunately I'm unfamiliar with Clutter. But I'm pretty sure it will be difficult to render an animation without a main loop running in any high level library.
I'd try to put the code that causes the delay into a separate thread and inform the main loop when the startup is done.
Something like this is what i use:
string splashfile = path_templ + "/splashimg.png";
GtkWidget *image=gtk_image_new_from_file(splashfile.c_str());
gtk_container_add(GTK_CONTAINER(SplashWindow), image);
gtk_widget_show_all(SplashWindow);
//Cycle through all iterations (refresh everything in the GUI)
while (gtk_events_pending()){
gtk_main_iteration();
}
sleep(1);
(... rest of code ...)
gtk_main ();
gdk_threads_leave ();
Especially that last part of while events pending is the key
i have created iphone game.When i pause the game using pause button i quit cliking quit button...
Now when i start the game again ..the previous counter i created using this code...
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTimerFunc) userInfo:nil repeats:YES];
the timer is 100 sec and moves to zero...now it start giving the difference of twwo ,,98,96,94
if i quit the game again ans start this time the difference will become of 4 96,92 ...its keep on increasing ....what is this issue?
kindly help
Sounds like "multi-tasking" and improperly placed initialization code.
You may not actually be "exiting" your application when you think you are. Double check that the application is not still running in the background after you press the "home" button.
Also, it could have something to do with your "quit" button as well - where it is not actually "quitting" but rather putting the application into the background?
My question in the previous post was-
I have a cocos2d game, and after I
exit the game and start it again, it
starts at the exact same point it was
before closing.why this is
happening??and not only this, my game
is landscapemode but when I start it
again from simulator it comes in
portrait mode,not it landscapemode like
first time.
Is there any way to prevent this?
Anyone will come to rescue me???
I found a solution.Is it good??
when I exit the game(by pressing the back button in iPhone simulator) this problem occurred. Now I found that if I press back button the following function is called-
(void)applicationWillResignActive:(UIApplication *)application
So I set (divide by zero) or something like this in -(void)applicationWillResignActive function so that when this function called the application crashes. Then if I restart the application everything comes from beginning(reset) which I want. Is it good solution or anything better?
*my applicationDidFinishLaunching was not called again when application is restart from simulator.
You should configure the Info.plist to indicate that you does not want your application run in Background.
The key is
UIApplicationExitsOnSuspend
If you need more detail you should look at this tutorial
If you have iOS 4.0 or higher, multitasking is causing your problem.