I am trying to create pause screen for my game. The game is developed upon continuous scrolling background (top to bottom) with obstacles which are moving independently (left to right).
Below is the code:
PauseScene.h
#interface PauseScene : CCNode {}
#property (nonatomic, retain) HelloWorldScene *mainScene;
//+ (PauseScene *)scene;
- (id) init;
#end
PauseScene.m
#implementation PauseScene
- (id) init {
if (self = [super init]) {
// Adding RESUME and QUIT button and few others to make the whole Pause screen
}
return self;
}
// Called the method when Resume button is selected
- (void) Resume:(id) sender {
//[[CCDirector sharedDirector] startAnimation];
//[[CCDirector sharedDirector] resume];
self.mainScene.userInteractionEnabled = YES;
[self.mainScene removeChild: self];
[self.mainScene StartGame]; // StartGame in MainScene contains the same code which commented out above, I will paste StartGame method later
}
// Quit method is called when the Quit button is pressed
- (void) Quit:(id) sender {
//[[CCDirector sharedDirector] resume];
//[[CCDirector sharedDirector] startAnimation];
self.mainScene.userInteractionEnabled = YES;
[self.mainScene removeChild: self];
NSLog(#"Pre Reload", nil);
//[self.mainScene scheduleOnce:#selector(ReloadGame) delay: 1.0f];
[self.mainScene ReloadGame]; // Reload method contains the code commented above
}
#end
PauseScene is mere node class.
StartGame and ReloadGame methods in MainScene file
- (void) StartGame {
// Create and display HUD layer, like, scores, navigation buttons, etc.
// Resuming the game
//[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
}
- (void) ReloadGame {
// Resume the game
//[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
[[CCDirector sharedDirector] replaceScene:[HelloWorldScene scene] withTransition:[CCTransition transitionFadeWithDuration:1.0f]];
}
The pause button is displayed in HUD layer of MainScene, so, when the pause button is clicked, we will pause the game and display the PauseScene
- (void)onPauseClicked:(id)sender {
// Code to display the pause node of this scene
// Pause the game
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] pause];
}
When I comment out that stopAnimation and pause part the Pause and Resume works perfect. But when I implement any of these, at the time of quitGame, replaceScene is not working somehow. I think this is because game goes in pause mode but it is not working even if I call replaceScene using scheduler on delay of 5-10 seconds.
And if I don't use these stopAnimation and pause, some sprites which are given CCAction are not being paused and goes on even when my game is paused.
Your help is appreciated because I am using Cocos2d 3.0 and unable to find any proper help.
Thanks #LearnCocos2D for your tips.
I am able to successfully implement pause/resume by just removing the code stopAnimation or startAnimation and pause and resume methods. I just replaced these all with paused property for the node.
To pause the game, need to set paused = YES for every node in the game and reverse to resume.
Related
I have tried to find a way of registering a touch event in cocos3d such as TOUCHESBEGAN but that does not exist, only
-(void) touchEvent: (uint) touchType at: (CGPoint) touchPoint {
NSLog(#"hello");
}
Bu that does not log "hello".
How can this be done?
Not sure if you are still interested with the answer, but here goes.
In order to use touchEvent in cocos3d, you need to enable touch in the CC3Layer (in the initializeControls method)
self.isTouchEnabled = YES;
Then you can use the touchEvent:(uint)touchType at:(CGPoint)touchPoint in the CC3Scene.
Is started to work for me when I have put this code in my CC3Layer subclass
-(void) initializeControls {
[self scheduleUpdate];
self.userInteractionEnabled = YES;
[self setTouchEnabled:YES];
}
I've been trying to resolve a problem for a long time, unfortunately i couldn't find a solution. I hope you can help me...
I'm trying to develop a turn based game. Player 1 is the user, player 2 is the iphone. I want to receive touches when it's the player1's turn.
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (self.playerNo==1) { //control player1
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
return YES;
}
//if it's not player1's turn, return NO
return NO;
}
When it's iphones turn, I want to disable touches during 5 seconds. I tried all the ways below, but no success.
-(void)iphonesTurn
{
[NSThread sleepForTimeInterval:5];
//
//game logic...
//
}
During 5 seconds I touch screen several times. I realised that all the touches looks like stored and the app reacts to all of my touches when it's the player1's turn.
How can I completely disable touches?
Thanks for your help.
I don't know where is located your snippet of code but if it runs into the main thread you must NEVER block (pause) it!
Try to do something like that (it works with the final version of cocos2d 2.0):
- (void)iphonesTurn
{
[[[CCDirector sharedDirector] touchDispatcher] setDispatchEvents:NO];
[self scheduleOnce:#selector(enableTouch) delay:5.0f];
}
- (void)enableTouch
{
[[[CCDirector sharedDirector] touchDispatcher] setDispatchEvents:YES];
}
I am writing a pause menu using a CCLayer. I need the layer to swallow touches so that you cannot press the layer below, however I also need to be able to use the buttons on the pause layer itself.
I can get the layer to swallow touches, but the menu won't work either.
Here is my code:
pauseLayer.m
#import "PauseLayer.h"
#implementation PauseLayer
#synthesize delegate;
+ (id) layerWithColor:(ccColor4B)color delegate:(id)_delegate
{
return [[[self alloc] initWithColor:color delegate:_delegate] autorelease];
}
- (id) initWithColor:(ccColor4B)c delegate:(id)_delegate {
self = [super initWithColor:c];
if (self != nil) {
NSLog(#"Init");
self.isTouchEnabled = YES;
CGSize wins = [[CCDirector sharedDirector] winSize];
delegate = _delegate;
[self pauseDelegate];
CCSprite * background = [CCSprite spriteWithFile:#"pause_background.png"];
[self addChild:background];
CCMenuItemImage *resume = [CCMenuItemImage itemFromNormalImage:#"back_normal.png"
selectedImage:#"back_clicked.png"
target:self
selector:#selector(doResume:)];
resume.tag = 10;
CCMenu * menu = [CCMenu menuWithItems:resume,nil];
[menu setPosition:ccp(0,0)];
[resume setPosition:ccp([background boundingBox].size.width/2,[background boundingBox].size.height/2)];
[background addChild:menu];
[background setPosition:ccp(wins.width/2,wins.height/2)];
}
return self;
}
-(void)pauseDelegate
{
NSLog(#"pause delegate");
if([delegate respondsToSelector:#selector(pauseLayerDidPause)])
[delegate pauseLayerDidPause];
}
-(void)doResume: (id)sender
{
if([delegate respondsToSelector:#selector(pauseLayerDidUnpause)])
[delegate pauseLayerDidUnpause];
[self.parent removeChild:self cleanup:YES];
}
- (void)onEnter {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES];
[super onEnter];
}
- (void)onExit {
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
-(void)dealloc
{
[super dealloc];
}
#end
why dont you just disable touches on the game layer?
like in the onEnter method disable the touches on the game layer..and onExit re enable them
something like
-onEnter{
gameLayer.isTouchEnabled=NO;
....
}
-onExit{
gameLater.isTouchEnabled=YES;
...
}
also you wont need CCTouchDispatcher
According to your code, the problem is the modal layer is swallowing events even if it's for the own children.
To solve this problem, you have to set the touch priority of the children even higher than the modal layer itself.
In other words, set the menu's touch priority value below modal layer's.
There are two solutions.
Simply override "CCMenu::registerWithTouchDispatcher()" method and set the priority higher from the beginning.
Change menu's touch priority using "setPriority" method of the touchDispatcher or "setHandlerPriority" of menu itself.
To achieve second solution, you have to pay attention to the timing.
"CCMenu::registerWithTouchDispatcher()" is called somewhere AFTER "onEnter" and "onEnterTransitionDidFinish".
So, use "scheduleOnce" or something like that.
Sample codes.
- (id) initWithColor:(ccColor4B)c delegate:(id)_delegate {
self = [super initWithColor:c];
if (self != nil) {
//your codes...... put CCMenu in instance
[self scheduleOnce:#selector(setMenuPriority:) delay:0];
}
return self;
}
- (void) setMenuPriority (ccTime)dt {
[[[CCDirector sharedDirector] touchDispatcher] setPriority:INT_MIN forDelegate:menu];
//priority "INT_MIN" because you set the layer's priority to "INT_MIN+1".
}
PS: My english is not so good, so if there are loose sentences, correction will be very pleased :)
The problem is, that the layer/node hierarchy is not considered when propagating touches.
The touches are handed from the touch handler with the smallest priority value to the ones with the highest.
The touches are not forwarded anymore, once one of the responders swallows the touch.
You can use an approach similar to CCMenu. CCMenu handles all touches and detects which CCMenuItemhas been selected, based on the position of these items.
If you implement this the same way, you let your PauseLayer handle and swallow all touches and use a seperate mechanism to determine which child element in your PauseLayer has been selected.
Example Implementation: CCMenu Subclass
I have implemented a solution in this repository:
https://github.com/Ben-G/MGWU-Widgets/tree/master/Projectfiles/Components/CCMenuBlocking
That component is a CCMenuSubclass that swallows all touches and does not forward them.
Example Implementation: CCNode
Here is a more general solution of a CCNode that swallows touches and only forwards them to its own children:
https://github.com/Ben-G/MGWU-Widgets/blob/master/Projectfiles/Components/Popup/PopUp.m
I have a home button on my scene which when pressed goes to the home menu. I use replaceScene to replace the current scene (Game Scene) with the HomeMenu Scene. For some reason the actions and sounds which are happening in the game scene are not stopped when I replace the scene. I have tried the following code but still when I am in the home menu I can hear the actions and sounds of the game scene playing.
// fired when the home menu is
clicked!
-(void) homeMenuClicked:(CCMenuItem *) item { NSLog(#"home menu clicked!");
CCScene *scene = [[CCDirector
sharedDirector] runningScene]; [scene
stopAllActions];
[self.layer stopAllActions];
[self unloadSoundEffects];
[[CCDirector sharedDirector]
replaceScene:[CCSlideInLTransition
transitionWithDuration:1.0
scene:[HomeScene scene]] ];
}
I must also add that the game layer also has a timer (NSTimer) object which starts in 2 seconds or something.
UPDATE 2:
Let me post some code! I think the problem is that when the player guess the correct answer the following method is invoked:
[self updateForCorrectAnswer];
Inside updateForCorrectAnswer I have a performSelector which is scheduled to fire in 6-7 seconds. I believe that performSelector is the culprit. If somehow can I stop that from being firing then the I think I will be fine.
[self performSelector:#selector(refreshScore) withObject:nil afterDelay:7.0];
You should not use NSTimer as cocos2d documents.
/* call scheduleUpdate in initializing */
[self scheduleUpdate];
It schedules update: method to be called every frame when this node is on the stage.
- (void)update:(ccTime)dt
{
/* This method is automatically called every frame. */
}
scheduleUpdateWithPriority:, schedule:interval: are also available.
And why don't you use like this instead of performSelector:withObject:afterDelay.
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:7], [CCCallFunc actionWithTarget:self selector:#selector(refreshScore)], nil]];
If you use this method "[self performSelector:withObject:afterDelay:]", cocos2d will not manage it in this run loop. You should use it instead of the previous one:
[self schedule:interval:];
Then in your "homeMenuClicked:" method, just call this:
[self.layer unschedule:#selector(refreshScore)];
I haven't tried it but I think it'll be better.
For more information you can see the documentation here.
My question is to look for design solution for pause/resume states (including all data info, which need save ) during cocos2d game.
Including following cases, but not limited:
1). User choose quit, then pop out one dialog for user to choose " quit directly", " pause " ;
2). Someone call in , pop out dialog for user to choose "quit " or " pause " game.
If choose "pause", everything which deserve saving, should be saved. Just like PC games do.
I know Director provides "pause" , "resume " , is that okay for this task ?
Thanks for anyone clues or comments.
Welcome for further discussing via email : apple.dev.sh#gmail.com
Here is a good example:
To pause:
- (void) applicationDidEnterBackground:(UIApplication *)application
{
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] pause];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] pause];
}
When resuming:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[CCDirector sharedDirector] stopAnimation]; // call this to make sure you don't start a second display link!
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
}