tags:

views:

223

answers:

2

So when I see ccTouchesBegan (or touchesBegan for that fact of the matter) I see something like this usually:

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
   UITouch* touch = [touches anyObject];
}

But, what I am not getting is how do you just detect if one object has been touched? For example how would I check if a specific CCSprite I declared has been touched?

Sorry if this is a newbish question but, I just don't understand and if you need more clarification just ask how I can clarify myself more.

A: 

I'm not familiar with cocoas2d but in the standard API it sends the touches first to the view touched and then up the view responder chain to a view that has a controller. If that controller does not handle the touch then it goes up to the next view until it ends up at the Window object.

See Responder Objects in the Responder Chain

The best place to trap touches for specific objects is in the object themselves. In the case of sprite-like view, the sprite itself most likely needs to respond to the touch e.g. by moving itself. If you need the touch to be communicated to another object, you should use the delegate pattern so that the sprite can tell its delegate how its been touched.

That last sentence sounded weird.

TechZen
A: 

I don't have the samples in front of me but there should be an example in the Cocos2D download package which demonstrates a touch event and how it propagates down to sprites.

Rob Segal