开发者

How can i detect whether a touch action touches on a sprite or not at zoom state?

First i have scaled the layer larger which contains the sprites. Now I need to sense touch on a sprite. I have tried as follows, but cant reach to goal-

CGRect tRect= [[aSprite displayedFrame] rect];
    if(CGRectContainsPoint(tRect, touchedPosition))
{
    NSLog(@"touched:>> touch at (%f,%f)",touchedPosition.x,touchedPosition.y);
    // Do something, maybe return kEventHandled;
}
else{
    NSLog(@"NOT touched: touch at (%f,%f)",touchedPosition.x,touchedPosition.y)开发者_如何学Go;
}

FYI: I have used cocos2d framework


First, you need to make sure you get the location from the UITouch correctly.

CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];

Second, you need to test your touch against the sprite's bounding box.

if (CGRectContainsPoint([sprite boundingBox], location)) {
    // The sprite is being touched.
}


Frank Mitchell is correct. Another approach would be to add your listening code to the sprite itself so that Cocos will do the work for you. It will only send the sprite ccTouchesBegan events if it is actually touched.


At last I have found the solution :), here is the code

CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];

CGPoint tapPosition = [self convertToNodeSpace:convertedLocation];

where 'self' is the sprite holder layer as I have specified before. This layer is listening the touch event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜