2011-11-25 74 views
1

我做了這個ccTouchBegin方法: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的表格。但現在它不工作,即使我嘗試觸摸整個屏幕,我也沒有收到任何NSLog。任何想法什麼是錯的?

回答

2
  1. Learn how to set a breakpoint。斷點允許您立即測試一個方法或一行代碼是否正在執行。
  2. 除非您使用CCTargetedTouchHandler作爲CCLayer中觸摸的默認處理程序,否則不會調用ccTouchBegan方法。要做到這一點,覆蓋registerWithTouchDispatcher在CCLayer類,但不調用父類的實現:

-(void) registerWithTouchDispatcher 
{ 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self 
                priority:0 
              swallowsTouches:NO]; 
} 

你並不需要刪除的委託。 CCLayer類爲您處理。

+0

這解決了我的問題 - 謝謝! – Greg