I have many object running and there is one quit button when clicked the game will pause and there will be a prompt message ask whether 'YES'/'NO' (to quit or not). If I click yes the game will quit else the game will resume back from the pause state.
how to pause game and resume back
(6 posts) (4 voices)-
Posted 2 years ago #
-
Hey yuen,
Are you running some type of main loop? if so you can do something like:
[self unschedule: @selector(mainLoop:)];
-LarsPosted 2 years ago # -
mainLoop? sorry i dont get it.
Here is my code:
@implementation attack
-(id)init
{
if((self=[super init]))
{// her for quit
//=======================================================================[MenuItemFont setFontSize:20];
[MenuItemFont setFontName:@"TrebuchetMS-Bold"];MenuItem *quit=[MenuItemFont itemFromString:@"Quit" target:self selector:@selector(newLocalScore:)];
menu=[Menu menuWithItems:quit,nil];
menu.position=ccp(225,35);
[self addChild:menu];}
return self;
}
//quit
-(void)newLocalScore:(id)sender
{//pause(); <- do i need to do this? the game pause but every object on the screen were pause! :-(
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Quit"];
[dialog setMessage:@"Do you want to Quit?"];
[dialog addButtonWithTitle:@"Yes"];
[dialog addButtonWithTitle:@"No"];
[dialog show];
[dialog release];}
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0) {
//do stuff
Scene *quit_scene=[Scene node];
[quit_scene addChild:[layerMenu node]];
[[Director sharedDirector] replaceScene:[FadeTransition transitionWithDuration:0.5f scene:quit_scene ]];
}}
@end
Posted 2 years ago # -
Well from what you showed us there isn't really a game.
Games usually have a gameLoop, it's a scheduled method that gets called every frame or every predefined number of seconds. This method is responsible to updating the game state, including: moving objects around, check for collisions, etc...What Korki meant is that whenever the user clicks the quit button, you unschedule the method, so the game doesn't move - nothing gets updated.
Cocos2D uses the schedule method, where the CocosNode knows how to summon a method every sometime (explained above), to create such a method you can use:
[self schedule:@selector(gameLoop:) interval:0.5]; //self is a reference to the class you call the method to, gameLoop is the method called, interval is the timeIf the user clicks quit, in quit method implement:
[self unscheduleSelector:@selector(gameLoop:)];I really think you should read up some more about game programming and obj-C, and check out the Wiki - Riq has a nice thing going there.
Hope this helps somehow.Posted 2 years ago # -
This is the way I do it:
// Pause button pressed. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Paused" message:@"Press the button to..." delegate:self cancelButtonTitle:@"Resume" otherButtonTitles:nil]; [alert show]; [[Director sharedDirector] pause];And later on:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { [[Director sharedDirector] resume]; }// Nicke
Posted 2 years ago # -
thanks Nicke ...
that is work..and thanks for natanava ,..
Posted 2 years ago #
Reply
You must log in to post.