2010-07-18 92 views
4

如何检测旋转的CCSprite上的触摸?Cocos2d:检测旋转精灵上的触摸?

我熟悉使用ccTouchesBegan和contentSize,anchorPoint等的一般技术,让sprite检测触摸是否在其边界内......但我不确定一旦sprite被旋转一些角度。

我想让精灵本身检测触摸(封装)并通过委托将事件报告给另一个对象。

如果有人有一些代码分享...会很好。

回答

6

尝试使用CCNode convertTouchToNodeSpaceAR:方法将点转换为旋转坐标,然后您可以对精灵边界进行比较。

我在CCNode上做了这个类,所以它可用于任何CCNode或子类。

@interface CCNode (gndUtils) 

// Lets a node test to see if a touch is in it. 
// Takes into account the scaling/rotation/transforms of all 
// the parents in the parent chain. 
// Note that rotation of a rectangle doesn't produce a rectangle 
// (and we are using a simple rectangle test) 
// so this is testing the smallest rectangle that encloses the rotated node. 
// This does the converstion to view and then world coordinates 
// so if you are testing lots of nodes, do that converstion manually 
// 
// CGPoint touchLoc = [touch locationInView: [touch view]]; // convert to "View" 
// touchLoc = [[CCDirector sharedDirector] convertToGL: touchLoc]; // move to "World" 
// and then use worldPointInNode: method instead for efficiency. 

- (BOOL) touchInNode: (UITouch *) touch; 

// allows a node to test if a world point is in it. 
- (BOOL) worldPointInNode: (CGPoint) worldPoint; 

@end 

和实现:

@implementation CCNode (gndUtils) 

- (BOOL) touchInNode: (UITouch *) touch 
{ 
    CGPoint touchLoc = [touch locationInView: [touch view]];   // convert to "View coordinates" from "window" presumably 
    touchLoc = [[CCDirector sharedDirector] convertToGL: touchLoc];  // move to "cocos2d World coordinates" 

    return [self worldPointInNode: touchLoc]; 
} 

- (BOOL) worldPointInNode: (CGPoint) worldPoint 
{ 
    // scale the bounding rect of the node to world coordinates so we can see if the worldPoint is in the node. 
    CGRect bbox = CGRectMake(0.0f, 0.0f, self.contentSize.width, self.contentSize.height); // get bounding box in local 
    bbox = CGRectApplyAffineTransform(bbox, [self nodeToWorldTransform]);  // convert box to world coordinates, scaling etc. 
    return CGRectContainsPoint(bbox, worldPoint); 
} 
@end 
+0

谢谢!工作就像一个魅力... – poundev23 2010-07-23 00:40:51

+0

无论如何要做到这一点在cocos2d 2?我收到一些错误。我认为有些东西因为写了这些东西而被弃用。谢谢! – Corey 2013-03-18 21:53:50

+0

适合我。这应该被构建到cocos2d中。 :-) – 2013-04-13 23:07:43

5

@爸爸的代码转换节点的BBOX到世界各地。对于旋转的节点,这扩展了bbox,并且可以在实际节点之外但在世界bbox内的触摸返回true。为了避免这种情况,将世界点转换为节点的坐标空间,并测试其中的本地点。

+1

我相信我遇到了你在这里描述的问题,但我不理解你提出的解决方案。你能详细说明吗? – mwright 2012-03-28 16:14:35

+0

好点,好建议 – Dad 2013-06-03 18:17:53

2

由于苦艾酒说 - 的poundev23真的只检查BB围绕旋转精灵代码。 我写正确的代码(但Cocos2dx - C++) - 希望它可以帮助别人:

CCSize size = this->getContentSize(); 
CCRect rect = CCRect(0, 0, size.width, size.height); 
CCPoint pt = touch->locationInView(); 
pt = CCDirector::sharedDirector()->convertToGL(pt); 
pt = CCPointApplyAffineTransform(pt, this->worldToNodeTransform()); 
bool b = CCRect::CCRectContainsPoint(rect, pt); 

有一个很好的代码!

编辑: 好的解决方案!我将它转换为Objective C.

- (BOOL) containsTouchLocation:(UITouch *) touch 
{ 
    CGSize size = self.contentSize; 
    CGRect rect = CGRectMake(0, 0, size.width, size.height); 
    CGPoint touchLocation = [touch locationInView: [touch view]]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
    touchLocation = CGPointApplyAffineTransform(touchLocation, self.worldToNodeTransform); 
    bool containsPoint = CGRectContainsPoint(rect, touchLocation); 

    return containsPoint; 
} 
+1

转换为Obj-C并添加为编辑。非常感谢。 – jarryd 2013-07-01 16:06:39