views:

91

answers:

0

Hi

I'm having problem with touch events.

I have a class (Inheritance from CCLayer) that return some sprites with touches events.

I have around 10 of this same class with different sprites. Kind like a domino.

The problem is that if I move the sprites I have defined on this class over another, they snap to each other.

My .h file:

@interface pieces2Sides : CCLayer {

CCSprite *pecaTop;
CCSprite *pecaBottom;

CGPoint offset;

CGPoint locOne;

}

@property (nonatomic, retain) CCSprite *pecaTop;
@property (nonatomic, retain) CCSprite *pecaBottom;

+(id)pecaWithFile:(NSString *)file pecaWithFile2:(NSString *)file2 andPosX:(NSInteger)x andPosY:(NSInteger)y;

-(id) initWithFiles:(NSString *)file initWithFiles2:(NSString *)file2 andPosX:(NSInteger)x andPosY:(NSInteger)y;

@end

and m file

@implementation pieces2Sides

@synthesize pecaTop, pecaBottom;


+(id) pecaWithFile:(NSString *)file pecaWithFile2:(NSString *)file2  andPosX:(NSInteger)x andPosY:(NSInteger)y{

return [[[self alloc] initWithFiles:file initWithFiles2:file2  andPosX:(NSInteger)x andPosY:(NSInteger)y]   autorelease];
}

-(id) initWithFiles:(NSString *)file  initWithFiles2:(NSString *)file2  andPosX:(NSInteger)x andPosY:(NSInteger)y{

if ((self = [super init])) {

isTouchEnabled = YES;

pecaTop = [CCSprite spriteWithFile:file  rect:CGRectMake(0, 0, 120, 120)];
pecaTop.position = ccp(x, y);
pecaBottom = [CCSprite spriteWithFile:file2 rect:CGRectMake(0, 0, 120, 120)];
pecaBottom.position = ccp(pecaTop.position.x+120, pecaTop.position.y);
[self addChild:pecaTop];
[self addChild:pecaBottom];
}

return self;
}


-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *one = [touches anyObject];

if(one) {
locOne = [[CCDirector sharedDirector] convertToGL:[one locationInView:one.view]];

if (CGRectContainsPoint(pecaTop.boundingBox, locOne)) {
offset = ccpSub(pecaTop.position, locOne); 
}

if (CGRectContainsPoint(pecaBottom.boundingBox, locOne)) {
offset = ccpSub(pecaBottom.position, locOne); 
}

}

}

 -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 1) {

if (CGRectContainsPoint(pecaTop.boundingBox, locOne)) {
UITouch *one = [touches anyObject];
locOne = [[CCDirector sharedDirector] convertToGL:[one locationInView:one.view]];
pecaTop.position = locOne;
pecaBottom.position = ccp(pecaTop.position.x+120, pecaTop.position.y);
}   
}  
}

@end

and I use this class in my init method like this:

pieces2Sides *piece1 = [pieces2Sides pecaWithFile:@"o.png" pecaWithFile2:@"o.png" andPosX:100 andPosY:100];
[self addChild:piece1 z:2];

What I'm doing wrong? Or missing?

Thanks