Reputation: 1844
I'm trying to enable touch for layers as many other people have suggested online:
hudLayer = [[[CCLayer alloc] init] autorelease];
[self addChild:hudLayer z:3];
gameLayer = [[[CCLayer alloc] init] autorelease];
[self addChild:gameLayer z:1];
gameLayer.isTouchEnabled = YES;
rileyLayer = [[[CCLayer alloc] init]autorelease];
[self addChild:rileyLayer z:2];
pauseMenu = [[[CCLayer alloc] init] autorelease];
[self addChild:pauseMenu z:4];
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:hudLayer priority:0 swallowsTouches:YES];
and my touchmethods are here:
- (BOOL)ccTouchBegan:(NSSet *)touch withEvent:(UIEvent *)event {
return TRUE;
}
- (void)ccTouchEnded:(NSSet *)touch withEvent:(UIEvent *)event {
if (!paused) {
ratMove = 0;
}
}
however this continually throws the error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Layer#ccTouchBegan override me'
The only reason I could find for this error online is if you weren't including the ccTouchBegan function, however I am, does anyone else know any other reasons for this error to appear?
Upvotes: 0
Views: 2325
Reputation: 9079
your function does not implement the appropriate signature. Try:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
// your stuff here
}
if you want multiple touch handling (your signature), you should addStandardDelegate instead of targetedTouchDelegate.
EDIT : and now in objective-c:
[[CCDirector sharedDirector].touchDispatcher addStandardDelegate:self priority:0];
There are two protocols implemented by the touch dispatcher. You are currently registering as a targetTouchDelegate but implementing the delegate methods of the standardDelegate. Use the line above to register if you want to keep your methods.
Edit 2 : and now the exact syntax of the protocols, straight from cocos's code. As you can see, no ccTouchBegan with NSSet (your signature) BUT INSTEAD ccTouchesBegan. Whichever handling method you prefer (targeted of standard), you must conform to the protocols below.
@protocol CCTargetedTouchDelegate
/** Return YES to claim the touch.
@since v0.8
*/
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
@optional
// touch updates:
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;
@end
/**
CCStandardTouchDelegate.
This type of delegate is the same one used by CocoaTouch. You will receive all the events (Began,Moved,Ended,Cancelled).
@since v0.8
*/
@protocol CCStandardTouchDelegate <NSObject>
@optional
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
@end
Upvotes: 0
Reputation: 10860
Subclass CCLayer to have hud layer, then inside it implement these methods.
You add your hud layer as targeted delegate, then it must implement at least ccTouchBegan:withEvent:
method. If you want your hud to be targeted delegate, make CCLayer subclass and implement there methods from targeted touch delegate protocol.
Upvotes: 1