maw
maw

Reputation:

Problem with cocos2D for iPhone and touch detection

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 which 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?

Upvotes: 12

Views: 19665

Answers (7)

Hossam Ghareeb
Hossam Ghareeb

Reputation: 7123

-Make your scene conforms to protocol CCTargetedTouchDelegate -Add This line to init of your scene:

[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];

-Implement these functions:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
 {
   return  YES;
 }
 -(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
  {
    //here touch is ended
  }

Upvotes: 0

Rony
Rony

Reputation: 1229

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

        //Add a new body/atlas sprite at the touched location
        CGPoint tapPosition;
        for( UITouch *touch in touches ) {
            CGPoint location = [touch locationInView: [touch view]];

            tapPosition = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:location]];     // get the tapped position





    }
}

think this can help you....

Upvotes: 1

Damo
Damo

Reputation: 31

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

Upvotes: 3

jjxtra
jjxtra

Reputation: 21180

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;

Upvotes: 5

Dennis
Dennis

Reputation:

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

Upvotes: 3

Genericrich
Genericrich

Reputation: 4651

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!

Upvotes: 11

keremk
keremk

Reputation: 2303

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

Upvotes: 3

Related Questions