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 6775283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:53:19+00:00 2026-05-26T15:53:19+00:00

i am using cocos2d and having many sprites and bodies. each sprite has its

  • 0

i am using cocos2d and having many sprites and bodies. each sprite has its body, and each kind of sprite has a tag. (see next) .

when 2 sprites are contact each other, i can discover that and i want them to disappear.

THE problem is that i have many sprites from the same kind(having the same tag) so when i clean a sprite, it does clean the last sprite that was created and not the one i want.

my function that create a sprites from the same kind is :

-(void)fruit1:(int) randomForCoordinates
{

    //define sprite
    fruit1=[CCSprite spriteWithFile:@"fruit1.png"];
    fruit1.tag=2;
    fruit1.position=ccp(randomForCoordinates,500);
    //add fruit
    b2BodyDef spriteBodyDef;
    spriteBodyDef.type =b2_dynamicBody;
    spriteBodyDef.position.Set(fruit1.position.x/PTM_RATIO,fruit1.position.y/PTM_RATIO);
    spriteBodyDef.userData = fruit1;
    fruit1body = world->CreateBody(&spriteBodyDef);

    b2PolygonShape spriteShape; //b2polygon-for box shape
    spriteShape.SetAsBox(fruit1.contentSize.width/PTM_RATIO/2,fruit1.contentSize.height/PTM_RATIO/2); //for b2polygon
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteShapeDef.density = 10.0;
    spriteShapeDef.isSensor = false;
    fruit1body->CreateFixture(&spriteShapeDef);
    [self addChild:fruit1];

}

which is tagged as 2.
when i see a contact with this :

//cotact listener 
    std::vector<MyContact>::iterator pos;
    for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos) 
    {
        MyContact contact = *pos;
        b2Body *bodyA=contact.fixtureA->GetBody();
        b2Body *bodyB=contact.fixtureB->GetBody();

        //check if collision between to bodies
        if( bodyA->GetUserData() !=NULL && bodyB->GetUserData() !=NULL)    //if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture))
        {
            CCSprite *actor = (CCSprite*)bodyA->GetUserData(); 
            CCSprite *actorb = (CCSprite*)bodyB->GetUserData();


            //touch
            if ([actorb tag] == 2  && [actor tag]== 1)
            {
            [fruit1.parent removeChild:fruit1 cleanup:YES];
            NSLog(@"touch1");

            }

now the sprite that disappear is not that one that touched here, but the one that was created after him ..

HOW can i erase a SPECIFIC sprite-the one that just touched another one ??
not the LAST one created ?

thanks a lot

  • 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-05-26T15:53:20+00:00Added an answer on May 26, 2026 at 3:53 pm

    If two sprites are Collide you remove any one like below:

     if( bodyA->GetUserData() !=NULL && bodyB->GetUserData() !=NULL)    //if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture))
        {
            CCSprite *actor = (CCSprite*)bodyA->GetUserData(); 
            CCSprite *actorb = (CCSprite*)bodyB->GetUserData();
    
    
            //touch
            if ([actorb tag] == 2  && [actor tag]== 1)
            {
    
                [self removeChild:actor cleanup:YES];
                //(or)
                [self removeChild:actorb cleanup:YES];
    
    
            NSLog(@"touch1");
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an app using cocos2d, Im moving some sprites and having animations when
I am using cocos2d-iphone to place Sprites onto a Layer to setup a game
I'm using cocos2d and I want to see if a specific string is in
In my game, which is using cocos2d, there is going to be many different
am having uiviewcontroller for some effects using in camera. am using cocos2d lot for
I am using cocos2d but there is a chance its not that which is
I'm using Cocos2d for all the animated sprite/transition stuff, but I'm not sure how
im using xcode4 and box2d/cocos2d. Im having some strange difficulties with runtime errors that
I'm using Cocos2d to render a sprite, and UIGestureRecognizers to allow the user to
I made an App using cocos2D that has a hierarchical structure. And I used

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.