I am having problem on achieving the same objective using SmallMike method. I am using TouchDispatcher to handle all my touches. Below are the snippet of the code - mostly base on SmallMike method except that onExit, I remove the touch.
********
- (void)onExit
{
[[TouchDispatcher sharedDispatcher] removeDelegate:self];
if(!m_paused)
{
[super onExit];
}
}
-(void) pause
{
if(m_paused)
{
return;
}
[self onExit];
m_paused = YES;
}
************************************
I add the pause layer from my game layer (when a sprite got touch, it add the new layer).
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
if ([self containsTouchLocation:touch aSprite:pauseItem])
{
GamePauseLayer *layer3 = [[GamePauseLayer alloc] init];
layer3.position = ccp(115,85);
[self addChild:layer3 z:5];
NSLog(@".....................still pause..................");
[self pause];
return YES;
}
}
**********************************************
In my GamePauseLayer I had,
-(id) init {
self = [super init];
if(self != nil) {
ColorLayer *c = [ColorLayer layerWithColor:ccc4(0x00, 0x00, 0x00, 0xFF) width:250 height:150];
// c.position = ccp(115,85);
[self addChild:c];
[c setOpacity:128];
MenuItem *item1 = [MenuItemFont itemFromString:@"Resume" target:self selector:@selector(resumeGame:)];
MenuItem *item2 = [MenuItemFont itemFromString:@"Exit" target:self selector:@selector(exitGame:)];
Menu *menu = [Menu menuWithItems:item1, item2, nil];
menu.position = ccp(0,0);
[menu alignItemsVertically];
[self addChild:menu];
}
return self;
}
- (void)onEnter
{
[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
NSLog(@".............I am swalloing the touches.....................");
[super onEnter];
}
- (void)onExit
{
[[TouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
**********************************************************************************************************
Issue that I had
Whenever I pause, it added the new pause layer on the scene. However, the new layer does not accept any touches. I can't figure out why the new layer does not accept any touches. Basically, new layer added and everything pause and menu item not responding to touches. My understanding is onEnter for the new pause layer will handle the touches.
Please help.