我正在一个类(Path)中创建一个CGRect。我已经验证了该类正在创建矩形。现在,我已经在类中构建了一个方法,理论上,当被问到触摸位置是否在矩形内时,该方法应该返回true。问题是,它总是返回false。我已经验证了该方法可以根据需要进行操作。相关代码如下:
Path.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
Path.mm
#import "Path.h"
@implementation Path
@synthesize startP;
@synthesize endP;
@synthesize pathRect;
-(id) initWithPoints:(CGPoint)sP :(CGPoint)eP {
if ((self = [super init])) {
startP = sP;
endP = eP;
pathRect = CGRectMake(startP.x-2, endP.y, 5, 480);
}
return self;
}
-(bool)touchWithinBounds:(CGPoint)touch {
touchP = touch;
if(CGRectContainsPoint([self pathRect], touch)) {
return true;
} else {
return false;
}
}
-(void) draw {
glColor4f(1.0f,1.0f,1.0f,1.0f);
glLineWidth(5.0f);
ccDrawLine(startP, endP);
}
-(void) dealloc {
[super dealloc];
}
@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];
然后在ccTouchesBegan中,我将触摸位置发送到touchWithinBounds。但是,当触摸矩形时,它不会报告为真,而是始终报告错误:
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]);
我之间是否缺少某种连接,或者我是否误解了这种互动如何在一起工作?
由于
答案 0 :(得分:0)
想出来。我得到的点是我错误地发现了我的矩形。