Last active
May 15, 2016 11:49
-
-
Save syuhari/5009847 to your computer and use it in GitHub Desktop.
A modal layer of cocos2d-x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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