Я создаю CGRect внутри класса (Path).Я проверил, что класс создает прямоугольник.Теперь я встроил в класс метод, который теоретически должен возвращать 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]);
Есть ли какая-то связь, которую я пропускаю между частями, или я неправильно понимаю, как взаимодействие работает все вместе?
Спасибо