How can i remove CCSprite on click which are came from right to left direction…?

i am using cocos2dx 3.0(c++) and have many sprites generate from on sprite named “objDown”. each generated sprite has a setTag() assign …no w how can i remove this sprites on touch …help will be appreciated…see this code and give me trick to resolve it…this is my code:

void Movement::addObj()
{
//Sprite came from RIGHT to LEFT

objDown = CCSprite::create(“sprite3.png”);

int minYRL = (objDown->getContentSize().height/1024) + 148 ;

int maxYRL = (size.height - objDown->getContentSize().height/1024)-20;

int rangeYRL = maxYRL - minYRL;

actualYLR = ( arc4random() % rangeYRL ) + minYRL;
objDown->setPosition(ccp(768*(size.width/768),actualYLR) );
objDown->setTag(333);
this->addChild(objDown);
objDown->setTag(3);
RL=objDown->getTag();
int minDurationRL = (int)10.0;
int maxDurationRL = (int)20.0;
int rangeDurationRL = maxDurationRL - minDurationRL;
int actualDurationRL = ( arc4random() % rangeDurationRL ) + minDurationRL;

CCFiniteTimeAction* actionMoveRL = CCMoveTo::create( (float)actualDurationRL,ccp(0 , actualYLR) );

CCFiniteTimeAction* actionMoveDoneRL = CCCallFuncN::create( this,callfuncN_selector(Movement::spriteMoveFinished));

objDown->runAction( CCSequence::create(actionMoveRL,actionMoveDoneRL, NULL) );

//-------

//Sprite came from LEFT to RIGHT
objDown = CCSprite::create(“sprite1.png”);
int minYLR = (objDown->getContentSize().height/1024)+148;
int maxYLR = (size.height - objDown->getContentSize().height/1024)-20;

int rangeYLR = maxYLR - minYLR;
actualYLR = ( arc4random() % rangeYLR ) + minYLR;

objDown->setPosition(ccp(0*(size.width/768),actualYLR) );
this->addChild(objDown);
objDown->setTag(1);
LR=objDown->getTag();
int minDurationLR = (int)10.0;
int maxDurationLR = (int)20.0;
int rangeDurationLR = maxDurationLR - minDurationLR;

int actualDurationLR = ( arc4random() % rangeDurationLR ) + minDurationLR;

CCFiniteTimeAction* actionMoveLR = CCMoveTo::create( (float)actualDurationLR,ccp(768*(size.width/768), actualYLR) );

CCFiniteTimeAction* actionMoveDoneLR = CCCallFuncN::create( this,callfuncN_selector(Movement::spriteMoveFinished));

objDown->runAction( CCSequence::create(actionMoveLR,actionMoveDoneLR, NULL) );
//-----end
}

—>only one sprite i used its “cocos2d::CCSprite *objDown;”…and this sprite came from RIGHT to LEFT and (2) LEFT TO RIGHT direction…I want to remove that sprite which on i click…so how can this possible

In this pic
Digit 1(sprite1.png) is Came from -> left
Digit 10(sprite10.png) is Came from -> top
Digit 3(sprite3.png) is Came from -> right
Digit 2(sprite2.png) is Came from -> bottom

–> All this sprite generate using only one - *cocos2d::CCSprite objDown this sprite

I can’t entirely understand what you are trying to do.

If you use setTag() then you can probably use removeChildByTag(<tag>); to remove it.

It looks like you are giving each sprite the same tag though. So each sprite coming from the left has the same tag, each from the right has the same tag, etc. etc. If yo do that all of the same tag will be removed.

Depending you might be able to call removeFromParentWithCleanup(true);

@slackmoehrle
see in this picture i tell you what is actual issue to me…consider “digit1”…digit1 is came from left to right…there are three digit1 in this picture…

problem is- when i am click on the first generated digit1(mean near to right side screen) then it does not remove…but at the same time when i am click on last generated digit1(mean near to left side 1’s) then result is remove the first generated digit1…means itself does not remove but effect is it remove the previous generated digit 1( when i am do double click on last generated digit1 then it remove two previous digit1 and after then after next click itself remove )…
what i want to do: i want to remove specific ‘digit1’ on click (for example-when i click on middle digit1 then it should be remove )
- hey friend,are you understand what is actual issue i have???

my whole code here : http://pastebin.com/hmUfpwUK

If I understand correctly, the problem is that, on touch, your code doesn’t remove the right Sprite.

The problem comes from that piece of code:

if(objDown->boundingBox().containsPoint(touchLocation))   
{               
   this->removeChildByTag(LR);           
}

The problem is that objDown is not the right sprite.

What you need to do is

  1. Store all the sprites that can be removed in a Vector<Sprite*>
  2. In the ccTouchBegan method, iterate through this Vector and find the one meeting previous condition
  3. Remove this Sprite specifically, and do not use tags, as @slackmoehrle explained
  1. should look like this:

    vector.push_back(objDown);
    And do that for every Sprite

  2. and 3) should look like this:

    for(auto obj : vector)
    {
     if(obj->boundingBox().containsPoint(touchLocation))   
        {               
           this->removeChild(obj);      
        }
    }

@Fradow. I suggested RemoveChildByTag() but did say it isn’t probably best since it will remove all with the same tag and his code was giving each of the same, the same tag.

1 Like

only last generated sprite(digit1) have focus to tag value…because it generated last …
main problem is all the sprite having same tag…i can not identify all sprite-digit1 by Tag…and ccTouchBegan work on only last generated digit1…when i am click on last generated sprite then it remove previous generated digit1…not effect in itself remove… i want that when i am click on any digit1 then itself it should be remove…

I would also suggest another approach. If you add tags as integer generated using a millisecond function (a function returning the number of milliseconds since the start of time 01 01 1970) you can use this number as a tag for the borning sprites.
As an example: when you call your function to generate the sprite, just before call the time function giving you something as 10789783489. Use this number as the tag of the sprite.
When you want to delete this sprite, you can use the removeChildByTag(); anyway, you should store all these numbers in a vector to know who are the sprites still alive on the screen.