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
및 구현 :
아빠의 코드는 세계로 노드의 BBOX 변환 @
@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
출처
2010-07-18 01:28:38
Dad
고마워요! 매력처럼 작동 ... – poundev23
어쨌든 이걸 cocos2d 2에서 할 수 있을까요? 약간의 오류가 발생했습니다. 나는 이것이 쓰여진 이후로 몇몇 것들이 사용되지 않을 것이라고 생각한다. 감사! – Corey
나를 위해 작동합니다. 이것은 cocos2d에 내장되어야합니다. :-) –