Skip to content

Instantly share code, notes, and snippets.

@syuhari
Last active May 15, 2016 11:49
Show Gist options
  • Save syuhari/5009847 to your computer and use it in GitHub Desktop.
Save syuhari/5009847 to your computer and use it in GitHub Desktop.
A modal layer of cocos2d-x
#include "ModalLayer.h"
using namespace cocos2d;
bool ModalLayer::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority, true);
CCSize visibleSize = pDirector->getVisibleSize();
CCSprite* frame = CCSprite::create("frame.png");
frame->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
this->addChild(frame);
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"tg_close_1.png",
"tg_close_2.png",
this,
menu_selector(ModalLayer::menuCloseCallback) );
pCloseItem->setPosition(ccp(
visibleSize.width/2+frame->getContentSize().width/2-pCloseItem->getContentSize().width/2-10,
visibleSize.height/2+frame->getContentSize().height/2-pCloseItem->getContentSize().height/2-10));
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu);
return true;
}
bool ModalLayer::ccTouchBegan(CCTouch* touch, CCEvent* event) {
// can not touch on back layers
return true;
}
void ModalLayer::menuCloseCallback(CCObject* pSender)
{
this->removeFromParentAndCleanup(true);
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->removeDelegate(this);
}
#include "cocos2d.h"
class ModalLayer : public cocos2d::CCLayer
{
public:
virtual bool init();
void menuCloseCallback(CCObject* pSender);
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
CREATE_FUNC(ModalLayer);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment