メインレイヤーがあります。そしてその中で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つの正方形が一度に呼び出されます。