onTouchMoved not working!
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
auto f = static_cast<Sprite*>(getChildByTag(DRAG_BODYS_TAG));
f->setPosition(touch->getLocation());
CCLOG("Begin");
auto location = touch->getLocation();
auto arr = m_World->getShapes(location);
PhysicsBody* body = nullptr;
for (auto& obj : arr)
{
if ((obj->getBody()->getTag() & DRAG_BODYS_TAG) != 0)
{
body = obj->getBody();
break;
}
}
if (body != nullptr)
{
Node* mouse = Node::create();
mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY));
mouse->getPhysicsBody()->setDynamic(false);
mouse->setPosition(location);
this->addChild(mouse);
PhysicsJointPin* joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location);
joint->setMaxForce(5000.0f * body->getMass());
m_World->addJoint(joint);
_mouses.insert(std::make_pair(touch->getID(), mouse));
return true;
}
return false;
}
void HelloWorld::onTouchMoved(Touch* touch, Event* event)
{
CCLOG("Moved");
auto it = _mouses.find(touch->getID());
if (it != _mouses.end())
{
it->second->setPosition(touch->getLocation());
}
}
void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
CCLOG("Ended");
auto it = _mouses.find(touch->getID());
if (it != _mouses.end())
{
this->removeChild(it->second);
_mouses.erase(it);
}
}
In output window
only
Begin