Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:00:52+00:00 2026-06-18T00:00:52+00:00

I am having 15 sprites in my layer.i added these sprite to mutable array

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:00:53+00:00Added an answer on June 18, 2026 at 12:00 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using cocos2d and having many sprites and bodies. each sprite has its
I have a sprite added to a layer. I am having a lot of
Problem Having grown tired of writing functions to move sprites and characters diagonally and
i have an app using cocos2d, Im moving some sprites and having animations when
I've just started using sprites to cut down HTTP requests and I'm having a
I am using AndEngine to create a game. I have randomly added sprites in
I'm having a problem with evenly moving sprites; Currently I'm using a while loop
I'm having an issue getting this CSS for a sprite sheet to work on
Having just added a new button in my web application, I get an error
I having problems setting the child index of a sprite... It works perfectly on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.