My Touch&Drag code is wrong. pls help!

i m making drag Sprite by touching Screen. it run but cancelled when i drag screen(Sprite).
i think method “onTouchMoved” is wrong. but i cant solve… pls tell me what is wrong.

bool HelloWorld::init()
{

if ( !LayerColor::initWithColor(Color4B(255,255,255,255)) )
{
    return false;
}

auto pMan = Sprite::create("Images/grossini.png");
pMan->setPosition(150, 150);
this->addChild(pMan);


return true;

}

void HelloWorld::onEnter()
{
Layer::onEnter();

auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);

listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);

}

void HelloWorld::onExit()
{
_eventDispatcher->removeEventListeners(EventListener::Type::TOUCH_ONE_BY_ONE);

Layer::onExit();

}

bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
auto p = touch->getLocation();
Size s = Director::getInstance()->getWinSize();
Rect rect = Rect(0, 0, s.width, s.height);

if (rect.containsPoint(p))
{
	return true;
}
return false;

}

void HelloWorld::onTouchMoved(Touch* touch, Event* event) // suspected method
{

pMan->setPosition(pMan->getPosition() + touch->getDelta());

}

void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
log(“touch Ended”);
}

What is the desired behavior?

I tried out your code and it currently does what I would expect it to do. Regardless of where you touch on the screen, it will drag the sprite in a relative way to how you drag your finger.

If you want to only be able to drag your sprite when you touch the sprite, you need to change your onTouchBegan. Currently, it just checks if the touch is anywhere on the visible screen (which is a little redundant). To change this behavior, you want something like this:

bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
    auto p = touch->getLocation();
    Size s = Director::getInstance()->getWinSize();
    Rect rect = pMan->getBoundingBox();
    
    if (rect.containsPoint(p))
    {
        return true;
    }
    return false;
}