//// 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()//{////}总结
以上是内存溢出为你收集整理的Cocos2d-x_一个简单的Cocos2d-x程序全部内容,希望文章能够帮你解决Cocos2d-x_一个简单的Cocos2d-x程序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)