Search code examples
iosxcodecocos2d-iphoneuitouchccsprite

Moving sprite from a group of sprites


I am having 15 sprites in my layer.i added these sprite to mutable array i want to move single sprite using ccTouchesMoved.when the touch ended ccTouchesEnded sprite move back to its starting point or its origin.

My coding:

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

    collection=[[NSMutableArray alloc]init];

    CCLayer *base=[CCSprite spriteWithFile:@"Base.png"];
    base.position=ccp(512,384);
    [self addChild:base];




    x=0;
    for(int i=1;i<=7;i++)
    {
        CCSprite *hole=[CCSprite spriteWithFile:@"ball.png"];
        hole.position=ccp(140+x,318);
        hole.tag=i;
    [self addChild:hole];
        hole.visible=YES;
        [collection addObject:hole];
        x=x+75;
    }

    self.isTouchEnabled=YES;

}
return self;
 }
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
location=[self convertToNodeSpace:location];
p=location;


}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"count:%i",[collection count]);
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];

location=[self convertToNodeSpace:location];


 for(CCSprite *s in collection)
 {
   if(CGRectContainsPoint([s boundingBox], location))
    s.position=ccp(location.x,location.y);

 }
}
 -(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for(CCSprite *s in collection)
{

        s.position=p;

 }
}

when i moving one sprite other sprites also visible on the layer not to be hide. please help me with code.


Solution

  • for(CCSprite *s in collection)
     {
       if(CGRectContainsPoint([s boundingBox], location))
        s.position=ccp(location.x,location.y);
    
     }
    

    Since all your sprites are in the same location, the above code is moving all sprites in that location.

    for(CCSprite *s in collection)
    {
        if(CGRectContainsPoint([s boundingBox], location))
        {
            s.position=ccp(location.x,location.y);
            break;
        }
    }
    

    The above code will break out of the loop once the first sprite is detected, hence should only move the first sprite in the array.

    Not sure if this is exactly what you want, but hope this helps.

    ----edit

    Personally what I would do is have a instance variable for a sprite.

    CCSprite * touchedSprite;
    

    and do the following...

    -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch=[touches anyObject];
        CGPoint location=[touch locationInView:[touch view]];
        location=[[CCDirector sharedDirector]convertToGL:location];
        location=[self convertToNodeSpace:location];
    
        for((CCSprite * sprite in collection)
        {
            if(CGRectContainsPoint([sprite boundingBox], location))
            {
                touchedSprite = sprite;
                p = sprite.position;
                break;
            }
        }
    }
    -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"count:%i",[collection count]);
        UITouch *touch=[touches anyObject];
        CGPoint location=[touch locationInView:[touch view]];
        location=[[CCDirector sharedDirector]convertToGL:location];
    
        location=[self convertToNodeSpace:location];
    
        touchedSprite.position = location;
    }
    
    -(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        touchedSprite.position = p;
    }
    

    i didn't test the code so i am not sure if it will work just by copying and pasting, but i think this will give you the right idea. There are obviously other ways of doing this, but i think its the simplest if you are to do bunch of stuff to the sprite that is being touched.