Reputation: 33
My problem is that I didn't find a solution to "pierce" through a UIScrollView so the ccLayer could recognize the ccTouch events
self.isTouchEnabled = YES;
[[NSBundle mainBundle] loadNibNamed:@"myLayer" owner:self options:nil];
...
- (void) registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN swallowsTouches:NO];
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [self convertToWorldSpace:[self convertTouchToNodeSpace:touch]];
Any ideas how to create a delegate or another solution to bypass the UI and talk with the cc?
Upvotes: 0
Views: 880
Reputation: 3230
I had this problem this morning with Cocos2D v1.0.0. My solution was to include the CCTouchDispatcher method call inside my init method for the layer, and then that layer, inside a UIView, would recognize touches.
-(id) init
{
if ((self = [super init]) != nil) {
// do stuff
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
return self;
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]];
NSLog(@"TouchBegan at x:%0.2f, y:%0.2f", location.x, location.y);
return YES;
}
An alternative solution would be to use the ccTouchesBegan method:
-(id) init
{
if ((self = [super init]) != nil) {
// do stuff
self.isTouchEnabled = YES;
}
return self;
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *thisTouch in touches) {
CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[thisTouch locationInView:[thisTouch view]]]];
NSLog(@"TouchesBegan at x:%0.2f, y:%0.2f", location.x, location.y);
}
}
Note that the two touch methods have different methods to let your application know it should respond to touches. You can't mix and match how you want to respond to touches, and which touches you want to observe.
Upvotes: 2