Maybe I got this wrong, but afaik it's only Layers that get the touch events. So, how's a good way to detect if a user is tapping or dragging a specific sprite?
How detect touch for specific sprite?
(7 posts) (4 voices)-
Posted 2 years ago #
-
In old versions of cocos, only
Layersgot the touch events. But now there's aTouchDispatcherthat lets you detect a touch on a specificSprite. Look at the "TouchesTest" demo in the examples that come with cocos 0.8. Paddle.h/.m is where you want to look.Posted 2 years ago # -
how is TouchDispatcher different from ccTouchBegan: / ccTouchMoved: ?
i'm touching sprites (I think) and not using the TouchDispatcher.
Please be gentle, I still dont understand most of this stuff.
Posted 2 years ago # -
Ah, the Paddle conforms to the TargetedTouchDelegate protocol, and registers itself as an object that handles touches in onEnter:
[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
Posted 2 years ago # -
Hm, looking further into this, I realized that it isn't the the TargetedTouchHandler that checks whether or not the touch was inside the sprite, but rather the sprite itself. See this in paddle.m:
- (BOOL)containsTouchLocation:(UITouch *)touch
{
return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
}- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
if (state != kPaddleStateUngrabbed) return NO;
if ( ![self containsTouchLocation:touch] ) return NO;state = kPaddleStateGrabbed;
return YES;
}So even though you conform to the protocol and add it as a targeted delegate, you'll probably still get events even though they were outside your sprite. Am I right?
Posted 2 years ago # -
Does this code apply for a round shape??
Posted 2 years ago # -
From what I understand, it does not. Maybe there's something like CGRectContainsPoint but for circles. Not sitting at mac now so cannot check.
Posted 2 years ago #
Reply
You must log in to post.