Cocos2d iphone Cocos2d:CGRect,UITouch-对象/类未在其边界内检测到位置

Cocos2d iphone Cocos2d:CGRect,UITouch-对象/类未在其边界内检测到位置,cocos2d-iphone,uitouch,cgrect,Cocos2d Iphone,Uitouch,Cgrect,我正在类路径中创建一个CGRect。我已经验证了类正在创建矩形。现在,我在类中构建了一个方法,理论上,当询问触摸位置是否在矩形内时,该方法应返回true。问题是,它总是返回false。我已经验证了该方法是否能够根据需要获得触感。相关代码如下: 路径h #import <Foundation/Foundation.h> #import "cocos2d.h" @interface Path : CCSprite { CGPoint startP; CGPoint en

我正在类路径中创建一个CGRect。我已经验证了类正在创建矩形。现在,我在类中构建了一个方法,理论上,当询问触摸位置是否在矩形内时,该方法应返回true。问题是,它总是返回false。我已经验证了该方法是否能够根据需要获得触感。相关代码如下:

路径h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Path : CCSprite {
    CGPoint startP;
    CGPoint endP;
    CGRect pathRect;
    CGPoint touchP;
}

-(id)initWithPoints:(CGPoint)sP :(CGPoint)eP;
-(bool)touchWithinBounds:(CGPoint)touch;

@property (nonatomic, assign) CGPoint startP;
@property (nonatomic, assign) CGPoint endP;
@property (nonatomic, assign) CGRect pathRect;
@end
在我的主要游戏场景中,我初始化一条路径并将其添加到可变数组中:

paths = [[NSMutableArray alloc] init];
        Path *path1 = [[[Path alloc] initWithPoints:ccp(winSize.width/3, 0.0) :ccp(winSize.width/3, winSize.height)] autorelease];
        [self addChild:path1 z:0];
        [paths addObject:path1];
然后在CCTouchesStart中,我将触摸位置发送到touchWithinBounds。但是当触摸矩形时,它总是报告false,而不是报告true:

UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: [touch locationInView:touch.view ]];
    for (Path *path in paths) {
        CCLOG(@"%d", [path touchWithinBounds:location]);
这些片段之间是否有某种联系我遗漏了,或者我是否误解了互动是如何一起工作的


谢谢

把它弄明白了。我把矩形的起点弄错了

UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: [touch locationInView:touch.view ]];
    for (Path *path in paths) {
        CCLOG(@"%d", [path touchWithinBounds:location]);