auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
auto sprite = this->getChildByTag(virTag)->getChildByTag(jumpTag);
listener->onTouchBegan = CC_CALLBACK_2(MLayer::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(MLayer::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(MLayer::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sprite); //该精灵享有触摸
return true;
}
bool MLayer::onTouchBegan(Touch* touch, Event* event)
{
auto target = static_cast<Sprite*>(event->getCurrentTarget());//获取当前的触摸目标(那个跳跃精灵)
Point locationInNode = target->convertToNodeSpace(touch->getLocation());//将本地坐标系转化为精灵坐标系(以精灵的左下角作为坐标原点)
Size s= target->getContentSize();//获取精灵的文本尺寸大小
Rect rect = Rect(0, 0, s.width, s.height);//获取精灵的矩形框(起始点为精灵的左下角)
if (rect.containsPoint(locationInNode))//判断触摸点是否在精灵的矩形框上
{
auto sprite2 =this->getChildByTag(cmsTag)->getChildByTag(catTag);
sprite2->setScale(0.2f);
}
return true;
}
void MLayer::onTouchMoved(Touch* touch, Event* event)
{
auto target = static_cast<Sprite*>(event->getCurrentTarget());//获取当前的触摸目标
Point locationInNode = target->convertToNodeSpace(touch->getLocation());//将本地坐标系转化为精灵坐标系(以精灵的左下角作为坐标原点)
Size s = target->getContentSize();//获取精灵的文本尺寸大小
Rect rect = Rect(0, 0, s.width, s.height);//获取精灵的矩形框(起始点为精灵的左下角)
if (rect.containsPoint(locationInNode))//判断触摸点是否在精灵的矩形框上
{
target->setPosition(target->getPosition() + touch->getDelta());//将捕获的精灵坐标设在移动结束的地方
}
}
void MLayer::onTouchEnded(Touch* touch ,Event* event)
{
auto sprite2 = this->getChildByTag(cmsTag)->getChildByTag(catTag);
sprite2->setScale(0.6f);
}