How to add Touch Event to sprites dynamically

I want to add touch event listener to individual boxSprite object only(not out side the boxSprite rect area ). also want to implement these below methods
1.onTouchBegan
2.onTouchMoved
3.onTouchEnded

I am not able to add any event listener to sprite objects.

 void MySpriteFun::distribute_Box(Ref *pSender)
 {
for (int i = 0; i < 15; i++)
{
    std:: string boxName =  "a_"+MyUtility::numberToString(i+1)+".png";
    boxSprite =  Sprite::createWithSpriteFrameName(boxName);
    boxSprite->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
    boxSprite->setScale(1.0f);
    boxBatchNode->addChild(boxSprite);
    
    bigBox.insert(i, boxSprite);
    
    auto scaleTo = ScaleTo::create(0.3f,0.75f);
    boxSprite->runAction(scaleTo);
    
    auto box_action =  MoveTo::create(0.5f, Vec2(visibleSize.width/7.5f+(i*58.5f),195));
    boxSprite->runAction(box_action);
       }
  }

Programmer Guide - Chapter 8

Thank you .
I have already gone through this chapter.Did not find any reference on the web and finally I asked this question.please check my question.I am struggling for individual sprite touch even.

its possible or not I don’t have any clue.

Your code isn’t how I would do it

boxSprite =  (Sprite *)Sprite::createWithSpriteFrameName(boxName);

why are you doing this?

and your code doesn’t create any listeners at all. Perhaps you should post more of what you are doing. You can add listeners in a for loop. It is a common practice.

Creating sprite from frame name.I have sprite sheet having 200 images and have xml(.plist) file(generated from TexturePacker)
std:: string boxName = “a_”+MyUtility::numberToString(i+1)+“.png”;

You can add listeners in a for loop. It is a common practice.
How? please post some code snippet.

For example in Android we have some thing like this:-

 boxViewArray[i].setOnTouchListener(new View.OnTouchListener() 
{    
   @Override
    public boolean onTouch(View v, MotionEvent event) 
    {

       }
    });

what is the type of boxSprite?

cocos2d::Sprite *boxSprite;
Sorry, My mistake.I have updated my code :-
boxSprite = (Sprite *)Sprite::createWithSpriteFrameName(boxName);
to
boxSprite = Sprite::createWithSpriteFrameName(boxName);

My problem is in adding touch event listener to individual sprites only. i don’t need touch event out side the sprite.

Yes, the link I sent you covers the listeners.

I don’t need touch event out side the sprite. In bellow code if I Touched out side the sprite touch listener is get called that is what I don’t need …how to block those touch event out side the sprite that is the real problem. I wanted to consume the touch event only and only by the sprites. I wanted to attach touch event to sprite objects only.

for (int i = 0; i < 15; i++)
{
    std:: string boxName =  "a_"+MyUtility::numberToString(i+1)+".png";
    boxSprite = Sprite::createWithSpriteFrameName(boxName);
    boxSprite->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
    boxSprite->setScale(1.0f);
    boxBatchNode->addChild(boxSprite);
    
    bigBox.insert(i, boxSprite);
    
    auto scaleTo = ScaleTo::create(0.3f,0.75f);
    boxSprite->runAction(scaleTo);
    
    auto box_action =  MoveTo::create(0.5f, Vec2(visibleSize.width/7.5f+(i*58.5f),195));
    boxSprite->runAction(box_action);
    
    auto listener1 = EventListenerTouchOneByOne::create();
    listener1->setSwallowTouches(true);
    
    // trigger when you push down
    listener1->onTouchBegan = [](Touch* touch, Event* event)
    {
        
        CCLOG("touched %s","yes");
        
        return true; // if you are consuming it
    };
    
    // trigger when moving touch
    listener1->onTouchMoved = [](Touch* touch, Event* event)
    {
        // your code
    };
    
    // trigger when you let up
    listener1->onTouchEnded = [=](Touch* touch, Event* event)
    {
        // your code
    };
    
    // Add listener
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, boxSprite);

    
}

Yes, you need to check if your Sprite was touched.


listener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event)
    {
        cocos2d::Vec2 p = touch->getLocation();
        cocos2d::Rect rect = this->getBoundingBox();
        
        if(rect.containsPoint(p))
        {
            // so you touched the Sprite, do something about it
            return true;
        }
        
        return false;
    };

or you can return just true and put this code in onTouchEnded

In Android and iOS native programming.we just attach the object to touch listener that we wanted to be touched no need to check whether the touch point is inside the object or not(done by sysytem automatically). I am just trying to do same thing.

However in cocos2dx we have to check the touch point whether its inside the object or not as the touch event is not attached to individual objects. The touch event attached to Scene Graph.

Touch events are added to the Scene Graph by either a fixed priority or a priority determined by its place in the Scene Graph. An event with a priority of 10 would get triggered before any thing with less priority. So you either need to consume it (and act upon) it or let is pass through to the next lowest priority.

If you have a listener on the parent Node, you need to then check what was touched in that Node when it is touched. The code would be in the parent Node class and you would do your checking there.