如何检测cocos2d中旋转的sprite上的触摸 [英] how to detect touch on rotated sprite in cocos2d
本文介绍了如何检测cocos2d中旋转的sprite上的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经旋转精灵到90.我检查了旋转的精灵的触摸位置如下:
I have rotated sprite to 90. I checked the touch location of the rotated sprite as follows:
matchsprite.rotation=90;
CGRect r=CGRectMake(matchstick.position.x, matchstick.position.y, matchstick.contentSize.height,matchstick.contentSize.width);
if(CGRectContainsPoint(r, location))
NSLog(@"Hii");
此代码中的错误是什么?我没有得到嗨。
What is the error in this code? I didnt get "Hii". How to detect whether we tap that rotated sprite or not?
推荐答案
这里有两个CCNode扩展添加到 Kobold2D 游戏引擎:
Here are the two CCNode extension (category) methods I've added to the Kobold2D game engine:
-(BOOL) containsPoint:(CGPoint)point
{
CGRect bbox = CGRectMake(0, 0, contentSize_.width, contentSize_.height);
CGPoint locationInNodeSpace = [self convertToNodeSpace:point];
return CGRectContainsPoint(bbox, locationInNodeSpace);
}
-(BOOL) containsTouch:(UITouch*)touch
{
CCDirector* director = [CCDirector sharedDirector];
CGPoint locationGL = [director convertToGL:[touch locationInView:director.openGLView]];
return [self containsPoint:locationGL];
}
测试点是否在精灵(或标签或任何其他节点)那么简单如下:
Testing if a point is on a sprite (or label or any other node) then is as simple as:
UITouch* uiTouch = [touches anyObject];
if ([aSprite containsTouch:uiTouch])
{
// do something
}
这篇关于如何检测cocos2d中旋转的sprite上的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文