iosobjective-ccocos2d-iphonegesturetouches

Touches in Cocos2d, some bug


I've written the following program, which has to do the following: when user touch moving sprite it has to be removed from the scene.

But, when I run my code the following thing happens: when I touch the uppest sprite it dissappears whith it neighbour. How can I fix it?

Here is the code.

UPD: I've tested this code on smaller *.png file, and everything works fine. But on bigger *.png file (smth like 200x200 pixels, iPhone simulator) I have a bug: object removes on touchEvent with its the nearest neighbour.

definition

    #import <Foundation/Foundation.h>
#import "cocos2d.h"


@interface GameplayLayer : CCLayer {

    NSMutableArray *arrayOfSprites;

}
@end
    #import "GameplayLayer.h"
@implementation GameplayLayer
    -(id)init
{
    self = [super init];
    self.isTouchEnabled=YES;
    arrayOfSprites=[[NSMutableArray alloc] init];

    if (self != nil) 
    {
        int j=100;

        for (int i=0; i<=2; i++) {
            [arrayOfSprites addObject:[CCSprite spriteWithFile:@"sv_anim_1-hd.png"]];
            [[arrayOfSprites objectAtIndex:i] setPosition:CGPointMake(j,j)];
            [self addChild:[arrayOfSprites objectAtIndex:i] z:0 tag:i];
            j+=100;
        }
        [self startRunning];
    }
    return self;                                 
}
    -(void)startRunning
{
    CGSize screenSize=[[CCDirector sharedDirector] winSize];
    for ( CCSprite * currentSprite in arrayOfSprites) 
    {
        [currentSprite runAction:[CCMoveTo actionWithDuration:10 position:CGPointMake(screenSize.height,screenSize.width/2)]];
    }
}
        -(void) registerWithTouchDispatcher
    {
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0
                                                  swallowsTouches:YES];
    }

        -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        return YES;
    }

        -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint locationOfTouch=[self convertTouchToNodeSpace: touch];

        for (CCSprite *currentSprite in arrayOfSprites)
        {
            if (CGRectContainsPoint(currentSprite.boundingBox, locationOfTouch))
            {
                NSLog(@"Sprite was touched");
                [self removeChild:currentSprite cleanup:YES];
            }
        }

    }
    @end

Solution

  • Try this function:

    -(CGRect)getSpriteRect:(CCNode *)inSprite
    {
        CGRect sprRect = CGRectMake(
                                    inSprite.position.x - inSprite.contentSize.width*inSprite.anchorPoint.x,
                                    inSprite.position.y - inSprite.contentSize.height*inSprite.anchorPoint.y,
                                    inSprite.contentSize.width, 
                                    inSprite.contentSize.height
                                    ); 
    
        return sprRect;
    }
    
    
    - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *myTouch = [touches anyObject];
        CGPoint location = [myTouch locationInView:[myTouch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];
    
    
        for (CCSprite *currentSprite in arrayOfSprites)
        {
          CGRect rect = [self getSpriteRect:currentSprite];
    
          if (CGRectContainsPoint(rect, location))
          {
              NSLog(@"Sprite was touched");
              [self removeChild:currentSprite cleanup:YES];
           }
         }
    }