只需要在HellowWorld模板代码init中加入
this->setPositionX(-100);
将Scene移动后会发现,点击关闭菜单没有反应。但如果点击scene移动前关闭菜单原来所在的位置,可以进到menuCloseCallback。
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
if (closeItem == nullptr ||
closeItem->getContentSize().width <= 0 ||
closeItem->getContentSize().height <= 0)
{
problemLoading("'CloseNormal.png' and 'CloseSelected.png'");
}
else
{
float x = origin.x + visibleSize.width - closeItem->getContentSize().width/2;
float y = origin.y + closeItem->getContentSize().height/2;
closeItem->setPosition(Vec2(x,y));
}
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
this->setPositionX(-100); //添加此行后closeItem显示的位置无法点击
MenuItem* Menu::getItemForTouch里应该有bug。
我在Menu::getItemForTouch中将rect的位置做相应的偏移即可修复该问题:
MenuItem* Menu::getItemForTouch(Touch *touch, const Camera *camera)
{
Vec2 touchLocation = touch->getLocation();
for (const auto &item: _children)
{
MenuItem* child = dynamic_cast<MenuItem*>(item);
if (nullptr == child || false == child->isVisible() || false == child->isEnabled())
{
continue;
}
Rect rect;
//rect.size = child->getContentSize();
//强制做和scene相同的位移
rect.setRect(-100, 0, child->getContentSize().width, child->getContentSize().height);
if (isScreenPointInRect(touchLocation, camera, child->getWorldToNodeTransform(), rect, nullptr))
{
return child;
}
}
return nullptr;
}
但是是写死的,请问各位在Menu::getItemForTouch中如何获取parent的位移呢?另外如果考虑如果有多个父节点移动又该如何修改?