前面的的别人写的,研究了一下有点懂了,然后自己仿造HelloWorld 写了一个,模板的是在AppDelegate中调用 scene ,我就在菜单按钮的回调函数里调用
.h 中
class Newscene : public cocos2d::Layer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* scene();
// a selector callback
void menuCloseCallback(Ref* sender);
void menuCloseCallback1(Ref* sender);
// implement the "static node()" method manually
CREATE_FUNC(Newscene);
};
cpp 中
Scene* Newscene::scene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
Newscene *layer = Newscene::create();
// add layer as a child to scene
scene->addChild(layer);
return scene;
}
bool Newscene::init()
{
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
auto origin = Director::getInstance()->getVisibleOrigin();
//添加一个标题层
auto cocoa=LabelTTF::create("HelpTxt", "Arial", TITLE_FONT_SIZE*2);
cocoa->setPosition(Point(visibleSize.width/2,visibleSize.height/2+cocoa->getContentSize().height));
this->addChild(cocoa,1);
//背景精灵
LayerColor* clo=LayerColor::create(Color4B(0,255,255,100),visibleSize.width,visibleSize.height);
this->addChild(clo);
//添加2个菜单按钮
auto closeIt=MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(Newscene::menuCloseCallback,this));
auto closeIt1=MenuItemLabel::create(LabelTTF::create("Back", "Arial", TITLE_FONT_SIZE));
closeIt1->setCallback(CC_CALLBACK_1(Newscene::menuCloseCallback1,this));
auto menuu=Menu::create(closeIt,closeIt1,NULL);
closeIt->setPosition(Point(visibleSize.width/2-30,visibleSize.height/2+20));
menuu->setPosition(Point(visibleSize.width/2,100));
this->addChild(menuu,1);
return true;
}
//退出help
void Newscene::menuCloseCallback(Ref* sender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
//返回原先场景 help
void Newscene::menuCloseCallback1(Ref* sender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
auto sc=HelloWorld::scene();
Director::sharedDirector()->replaceScene(TransitionRotoZoom::create(1.0,sc));
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
//调用一个新的场景
void HelloWorld::menuCloseCallback2(Ref* sender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
auto sc=Scene::create();
Newscene* la=Newscene::create();
sc->addChild(la);
Director::sharedDirector()->replaceScene(CCTransitionFadeTR::create(1.0,sc));
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}