2012-08-24 79 views
0

我有一个主图层。并且有9个子层添加。 例如板的主层并添加9层正方形。独立检测特定图层上的触摸

我想检测特定位置上的触摸。

但它只检测第一个。 Plzz帮助我解决问题。

Here is a some code. 
// 
// Board.m 
// Tic Tac Toe 
// 
// Created by Waqas Naseem on 8/23/12. 
// Copyright 2012 __MyCompanyName__. All rights reserved. 
// 

    #import "Board.h" 


    @implementation Board 

    @synthesize squares; 

    - (id)init 
    { 

     self = [super initWithColor:ccc4(255, 255, 255, 255) width:300 height:300]; 
     if (self) { 
      // Initialization code here. 
      index=0; 


      for (int i=0; i<9; i++) { 

       [self addSquare]; 
      } 





     } 

     return self; 
    } 

    -(void)addSquare 
    { 

     Square *square=[[Square node] retain];   
     [square setPosition:ccp((index%3)*100, (index/3)*100)]; 
     //[square setPosition:ccp(100, 0)]; 
     [self addChild:square]; 
     index++; 
    } 
    -(BOOL)isTouchEnabled 
    { 
     return YES; 
    } 
    -(void)setIsTouchEnabled:(BOOL)isTouchEnabled 
    { 

    } 

    -(void)squaretouched 
    { 
     NSLog(@"Square touched"); 
    } 
    @end 


    @implementation Square 

    @synthesize state; 

    -(id)init 
    { 
     self=[super initWithColor:ccc4(255, 255, 255, 255) width:100 height:100]; 

     if(self) 
     { 
      isTouchEnabled_=YES; 
      //[self setRotation:30]; 

      CCSprite *square=[CCSprite spriteWithFile:@"square.png"]; 

      xImg=[CCSprite spriteWithFile:@"myCross.png"]; 

      oImg=[CCSprite spriteWithFile:@"myO.png"]; 



      [square setPosition:ccp(50, 50)]; 

      [xImg setPosition:ccp(50, 50)]; 

      [oImg setPosition:ccp(50, 50)]; 

      [xImg setVisible:NO]; 

      [self addChild:square]; 

      [self addChild:xImg]; 
      [self addChild:oImg]; 

      [self setOpacity:255]; 




     } 
     return self; 
    } 



    -(BOOL)handleTouch:(CGPoint)location 
    { 
     NSLog(@"Me touched "); 
     return YES; 
    } 

    @end 

    And this is the TouchLayerColor class 

    // 
    // TouchAbleColorLayer.m 
    // Tic Tac Toe 
    // 
    // Created by Waqas Naseem on 8/24/12. 
    // Copyright 2012 __MyCompanyName__. All rights reserved. 
    // 

    #import "TouchAbleColorLayer.h" 

    @implementation TouchAbleColorLayer 

    - (id)init 
    { 
     self = [super init]; 
     if (self) { 
      // Initialization code here. 
     } 

     return self; 
    } 
    -(CGRect) rect 
    { 
     float h=[self contentSize].height; 
     float w=[self contentSize].width; 

     //convert to origional locaiton 

     CGPoint pos=[self convertToWorldSpace:CGPointZero]; 

     //CGPoint pos=[self convertToNodeSpace:CGPointZero]; 

     //CGPoint pos=ccp(50, 100); 

     float x=pos.x; 
     float y=pos.y; 

     return CGRectMake(x, y, w, h); 
    } 
    -(BOOL)handleTouch:(CGPoint)location 
    { 
     // dont hanle touch here 
     return NO; 
    } 

    -(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     NSLog(@"Coming in Touch"); 
     if(!self.visible || !self.isTouchEnabled) 
     { 
      NSLog(@"Coming in Touch if"); 
      return ; 
     } 
     UITouch *touch=[touches anyObject]; 
     CGPoint location=[touch locationInView:[touch view]]; 

     //convert the touch location from uikit to OpenGL Coordinates 

     location=[[CCDirector sharedDirector] convertToGL:location]; 

     // did touch Happen in our rectangle ? 

     if(CGRectContainsPoint([self rect], location)) 
     { 
      [self handleTouch:location]; 
     } 


    } 

    @end 

当我点击9平方的任何方形触摸方法一次被调用。

回答

0

我不知道我有你的问题。无论如何,您可以检测图层中的触摸,并在触摸处理程序中遍历儿童列表并将触摸位置传递给每个孩子。当然你的孩子们的班级都应该实施一个与被调用的方法相同的接口(即detectedTouch)。这几乎就像一个可靠链模式http://it.wikipedia.org/wiki/Chain-of-responsibility_pattern