// // AppDelegate.h // #ifndef _APP_DELEGATE_H_ #define _APP_DELEGATE_H_ #include "cocos2d.h" class AppDelegate : private cocos2d::CCApplication { public: AppDelegate(); virtual ~AppDelegate(); virtual bool applicationDidFinishLaunching(); virtual void applicationDidEnterBackground(); virtual void applicationWillEnterForeground(); }; #endif
// // AppDelegate.cpp // #include "AppDelegate.h" #include "HelloWorldScene.h" USING_NS_CC; AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate() { } bool AppDelegate::applicationDidFinishLaunching() { // 初始化导演和OpenGL CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); // 导演拥有OpenGLView pDirector->setOpenGLView(pEGLView); // 是否显示FPS pDirector->setDisplayStats(true); // 设置FPS刷新的频率 pDirector->setAnimationInterval(1.0 / 60); // 创建场景 CCScene *pScene = HelloWorld::scene(); // 导演调用场景开始运行 pDirector->runWithScene(pScene); return true; } // App进入后台 void AppDelegate::applicationDidEnterBackground() { CCDirector::sharedDirector()->stopAnimation(); // 如果你使用简单的声音引擎,必须设置声音暂停 // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); } // App进入前台 void AppDelegate::applicationWillEnterForeground() { CCDirector::sharedDirector()->startAnimation(); // 如果你使用简单的声音引擎,必须设置声音恢复 // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); }
// // HelloWorldScene.h // #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); void menuCloseCallback(CCObject* pSender); void closeIconItem(CCObject *pSender); CREATE_FUNC(HelloWorld); }; #endif
// // HelloWorldScene.cpp // #include "HelloWorldScene.h" USING_NS_CC; CCScene* HelloWorld::scene() { // 初始化场景 CCScene *scene = CCScene::create(); // 初始化层 HelloWorld *layer = HelloWorld::create(); layer->setTouchEnabled(true); // 把层添加到场景 scene->addChild(layer); // 返回场景 return scene; } // 在“init”中初始化你的对象 bool HelloWorld::init() { // 首先初始化基类 if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 创建一个退出按钮,为菜单项 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2,origin.y + pCloseItem->getContentSize().height/2)); // 创建一个菜单,把菜单项添加进去 CCMenu* pMenu = CCMenu::create(pCloseItem,NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu,1); ///////////////////////////// // 创建一个标签 CCLabelTTF* pLabel = CCLabelTTF::create("Hello World","Arial",24); // 设置标签的位置 pLabel->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height - pLabel->getContentSize().height)); // 添加到层中 this->addChild(pLabel,1); ///////////////////////////// // 创建精灵,也就是背景图片 CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // 设置精灵位置 pSprite->setPosition(ccp(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y)); // 添加到层中 //this->addChild(pSprite,0); ///////////////////////////// // 创建自己的精灵 CCMenuItemImage *pIconItem = CCMenuItemImage::create("111.png","222.png",menu_selector(HelloWorld::closeIconItem)); pIconItem->setPosition(ccp(visibleSize.width/2.0,visibleSize.height/2.0)); // 创建一个菜单,把菜单项添加进去 CCMenu *pMenuIcon = CCMenu::create(pIconItem,NULL); pMenuIcon->setPosition(CCPointZero); this->addChild(pMenuIcon); return true; } void HelloWorld::closeIconItem(CCObject *pSender) { CCLog("按钮回调函数!"); } // 按钮回调函数 void HelloWorld::menuCloseCallback(CCObject* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); #else // CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // exit(0); #endif #endif }
// // IconLayer.h // #ifndef HelloWorld_IconLayer_h #define HelloWorld_IconLayer_h #include "cocos2d.h" class IconLayer : private cocos2d::CCLayer { public: //virtual bool init(); static cocos2d::CCScene* scene(); }; #endif
// // IconLayer.cpp // #include "IconLayer.h" USING_NS_CC; cocos2d::CCScene * IconLayer::scene() { CCScene *scene = CCScene::create(); cocos2d::CCLayer *layer = IconLayer::create(); scene->addChild(layer); return scene; } //bool IconLayer::init() //{ // //}