cocos2d enable touch in second scene
i have menu with buttons when touch the Play button game go to another scene but Touch don't work there i'm write self.isToucheE开发者_开发知识库nabled=YES; in init method and add in onEnter method [[CCTouchDispatcher sharedDispatcher] setDispatchEvents:YES];
but that don't work pleas help why i can enable touch
You must put the following code:
-(void) onEnter {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
inside the scene you want to enable the TouchDispatcher on, then in the same scene make sure you enter this:
-(void) onExit {
[[CCTouchDispatcher sharedDispatcher] removeDelegate: self];
}
and the touch should register in every scene that you have the above code in.
This is an example of how you define a menu in cocos2d (source):
CCMenuItem *starMenuItem = [CCMenuItemImage temFromNormalImage:@"ButtonStar.jpg" selectedImage:@"ButtonStarSel.jpg" target:self selector:@selector(starButtonTapped:)];
starMenuItem.position = ccp(60, 60);
CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
starMenu.position = CGPointZero;
[self addChild:starMenu];
If you need more help, please provide the code that you are using to create the menu.
You also have to add UIGestureRecognizerDelegate interface in your CCLayer in your header!
e.g.:
@interface YourScene : CCLayer <UIGestureRecognizerDelegate> {
}
In the layer use:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
Not:
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
精彩评论