我有一个主层。并且有 9 个子层添加。例如,一个主层板并添加了 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
当我单击任何 9 个正方形的正方形触摸方法时,会立即调用。