Ios 独立检测特定层上的触摸

Ios 独立检测特定层上的触摸,ios,cocos2d-iphone,Ios,Cocos2d Iphone,我有一个主层。其中有9个子层添加。 e、 g一层主板,增加9层正方形 我想检测特定位置的触摸 但它只检测到第一个。请帮我练习 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" @

我有一个主层。其中有9个子层添加。 e、 g一层主板,增加9层正方形

我想检测特定位置的触摸

但它只检测到第一个。请帮我练习

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
下面是一些代码。
//
//董事会主席
//抽搐
//
//由Waqas Naseem于2012年8月23日创建。
//版权所有2012年uu MyCompanyName uuu。版权所有。
//
#输入“Board.h”
@执行委员会
@合成正方形;
-(id)init
{
self=[super initWithColor:ccc4(255、255、255、255)宽度:300高度:300];
如果(自我){
//这里是初始化代码。
指数=0;

对于(int i=0;i我不确定我是否了解您的问题。无论如何,您可以在层中检测触摸,并在触摸处理程序中,迭代子对象列表并将触摸位置传递给每个子对象。当然,您的子对象类都应使用调用的方法实现一个公共接口(即detectedTouch).这很像一系列的责任模式