views:

7642

answers:

7

I just don't get it. I use cocos2d for development of a small game on the iPhone/Pod. The framework is just great, but I fail at touch detection. I read that you just need to overwrite the proper functions (e.g. "touchesBegan" ) in the implementation of a class wich subclasses CocosNode. But it doesn't work. What could I do wrong?

the function:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{NSLog(@"tickle, hihi!");}

did I get it totally wrong?

Thank you!

+2  A: 

In order to detect touches, you need to subclass from UIResponder (which UIView does as well) . I am not familiar with cocos2D, but a quick look at the documentation reveals that CocosNode does not derive from UIResponder.

Upon further investigation, it looks like Cocos folks created a Layer class that derives from CocosNode. And that class implements the touch event handlers. But those are prefixed by cc.

See http://code.google.com/p/cocos2d-iphone/source/browse/trunk/cocos2d/Layer.h

Also see menu.m code and the below blog post article for more info on this:

http://blog.sapusmedia.com/2008/12/cocos2d-propagating-touch-events.html

keremk
second link not working (anymore)
Allisone
+9  A: 

Layer is the only cocos2d class which gets touches.

The trick is that ALL instances of Layer get passed the touch events, one after the other, so your code has to handle this.

I did it like this:

-(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
CGPoint cLoc = [[Director sharedDirector] convertCoordinate: location];

float labelX = self.position.x - HALF_WIDTH;
float labelY = self.position.y - HALF_WIDTH;
float labelXWidth = labelX + WIDTH;
float labelYHeight = labelY + WIDTH;

if( labelX < cLoc.x &&
 labelY < cLoc.y &&
 labelXWidth > cLoc.x &&
 labelYHeight > cLoc.y){
  NSLog(@"WE ARE TOUCHED AND I AM A %@", self.labelString);
  return kEventHandled;
 } else {
  return kEventIgnored;
 }

}

Note that the cocos2d library has a "ccTouchesEnded" implementation, rather than the Apple standard. It allows you to return a BOOL indicating whether or not you handled the event.

Good luck!

Genericrich
you can make any CCNode class receive touches! Use, for example: [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
GamingHorror
class must implement one of the two TouchDelegate protocols
GamingHorror
A: 

Thank you both, now I use a Layer-instance and the touches work! But somehow I don't get the current position of the touch, although I did it exactly as in some example.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

UITouch *touch=[touches anyObject];

CGPoint location=[touch locationInView:[touch view]];

NSLog(@"%i",location.x);

NSLog(@"%i",location.y);

}

The logged values are always 0. What am I doing wrong? And btw how do I insert my code here so it looks like in the post above?

Thanks!

Try changing your NSLog statement to NSLog(@"%f",location.x); CGPoint is a struct of two floats, not two ints like your NSLog statements above are expecting.
Adam Byram
Also, to get the x/y coordinates to be 'relative to your scene' use [[Director sharedDirector] convertCoordinate:location] ... otherwise, you'll get really funky x/y coordinates.
David Higgins
+2  A: 

maw, the CGPoint struct members x,y are floats. use @"%f" to format floats for printf/NSLog.

+1  A: 

Have you added this to your layers init method?

 // isTouchEnabled is an property of Layer (the super class).
 // When it is YES, then the touches will be enabled
 self.isTouchEnabled = YES;

 // isAccelerometerEnabled is property of Layer (the super class).
 // When it is YES, then the accelerometer will be enabled
 self.isAccelerometerEnabled = YES;
John JJ Curtis
+1  A: 

If you use the 0.9 beta of cocos2D it has a really simple touch detection for CocosNodes. The real beauty of this new detection is that it handles multiple touch tracking really well.

An example of this can be found here

http://code.google.com/p/cocos2d-iphone/source/browse/#svn/trunk/tests/TouchesTest

Damo
A: 

wow! Thanks!!!!! :)

Bobby