我做了这个ccTouchBegin方法:
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
CGPoint pos = [self getChildByTag:1].position;
CGSize size = [self getChildByTag:1].contentSize;
float padding = 3;
float w = size.width+padding*2;
float h = size.height+padding*2;
// first row
CGRect rect1 = CGRectMake(pos.x-w/2, pos.y-h/2, w/3, h/3);
CGRect rect2 = CGRectMake(pos.x-(w*(2/3)/2), pos.y-h/2, w/3, h/3);
CGRect rect3 = CGRectMake(pos.x+(w*(2/3)/2), pos.y-h/2, w/3, h/3);
//second row
CGRect rect4 = CGRectMake(pos.x-w/2, pos.y-(h*(2/3)/2), w/3, h/3);
CGRect rect5 = CGRectMake(pos.x-(w*(2/3)/2), pos.y-(h*(2/3)/2), w/3, h/3);
CGRect rect6 = CGRectMake(pos.x+(w*(2/3)/2), pos.y-(h*(2/3)/2), w/3, h/3);
//third row
CGRect rect7 = CGRectMake(pos.x-w/2, pos.y+(h*(2/3)/2), w/3, h/3);
CGRect rect8 = CGRectMake(pos.x-w/2, pos.y+(h*(2/3)/2), w/3, h/3);
CGRect rect9 = CGRectMake(pos.x-w/2, pos.y+(h*(2/3)/2), w/3, h/3);
if (CGRectContainsPoint(rect1, location)) {
NSLog(@"rect1");
} else if (CGRectContainsPoint(rect2, location)) {
NSLog(@"rect2");
} else if (CGRectContainsPoint(rect3, location)) {
NSLog(@"rect3");
} else if (CGRectContainsPoint(rect4, location)) {
NSLog(@"rect4");
} else if (CGRectContainsPoint(rect5, location)) {
NSLog(@"rect5");
} else if (CGRectContainsPoint(rect6, location)) {
NSLog(@"rect6");
} else if (CGRectContainsPoint(rect7, location)) {
NSLog(@"rect7");
} else if (CGRectContainsPoint(rect8, location)) {
NSLog(@"rect8");
} else if (CGRectContainsPoint(rect9, location)) {
NSLog(@"rect9");
}
return YES;
}
简单来说,它就是接触点,然后获得孩子的contentsize和位置,然后将孩子所在的位置划分成3x3的表格。但现在它无法正常工作,即使我尝试触摸整个屏幕,也无法获得任何NSLogs。你知道怎么了吗?
最佳答案
ccTouchBegan
方法。为此,请在CCLayer类中重写registerWithTouchDispatcher,但不要调用 super 实现:-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
priority:0
swallowsTouches:NO];
}
您不需要删除委托。 CCLayer类为您处理。
关于objective-c - ccTouchBegin接触点未正确读取,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8269901/