Cocos2d iphone Cocos2d:检测旋转精灵上的触摸?

Cocos2d iphone Cocos2d:检测旋转精灵上的触摸?,cocos2d-iphone,Cocos2d Iphone,人们如何检测旋转的雪碧上的触摸 我熟悉使用CCTouchesBegind和contentSize、anchorPoint等让sprite检测触摸是否在其范围内的一般技术。。。但我不知道如何继续一旦精灵已经旋转了一些角度 我希望精灵本身能够检测到触摸(封装),并通过代理将事件报告给另一个对象 如果有人有一些代码要共享。。。非常好。尝试使用CCNode convertTouchToNodeSpaceAR:方法将点转换为旋转坐标,然后可以比较精灵边界 我在CCNode上将其作为一个类别,因此它可用于任

人们如何检测旋转的雪碧上的触摸

我熟悉使用CCTouchesBegind和contentSize、anchorPoint等让sprite检测触摸是否在其范围内的一般技术。。。但我不知道如何继续一旦精灵已经旋转了一些角度

我希望精灵本身能够检测到触摸(封装),并通过代理将事件报告给另一个对象


如果有人有一些代码要共享。。。非常好。

尝试使用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

@爸爸的代码将节点的bbox转换为世界。对于旋转的节点,这将扩展bbox,并且对于实际节点外部但在世界bbox内部的触摸,可以返回true。为了避免这种情况,请将世界点转换为节点的坐标空间,并在那里测试局部点。

正如苦艾酒所说的-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);
有一个很好的代码

编辑: 很好的解决方案!我把它转换成了目标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;
}

我相信我遇到了你在这里描述的问题,但我不理解你提出的解决方案。你能详细说明一下吗?在CoCoS2D2上怎么做?我有一些错误。我想自从这本书写下来后,有些东西就被弃用了。谢谢对我有用。这应该内置到cocos2d中。:-)请参阅下面的Sofi软件有限责任公司的备选建议,该建议可能更快更准确。向上投票。转换为Obj-C并将其作为编辑添加。很好用,谢谢。