cocos2dx(2)代码基本结构
2013-06-04 21:37
183 查看
入口类,cocos2d::CCApplication类统一了各平台的差异
AppDelegate.h
AppDelegate.cpp
HelloWorldScene.h
HelloWorldScene.cpp
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 // __APP_DELEGATE_H__
AppDelegate.cpp
bool AppDelegate::applicationDidFinishLaunching() { CCDirector *pDirector = CCDirector::sharedDirector();//初始化导演② pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());//绑定opengl窗口 pDirector->setDisplayStats(true);//是否显示fps pDirector->setAnimationInterval(1.0 / 60);//fps默认为1.0 / 60 CCScene *pScene = HelloWorld::scene();//初始化场景③ pDirector->runWithScene(pScene);//场景run return true; }
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 ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);//按键处理 CREATE_FUNC(HelloWorld);⑤ }; #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h" using namespace cocos2d; CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { scene = CCScene::create(); CC_BREAK_IF(! scene); HelloWorld *layer = HelloWorld::create();④ CC_BREAK_IF(! layer); scene->addChild(layer); } while (0); return scene; } bool HelloWorld::init()⑥ { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); CCMenuItemImage *pCloseItem = CCMenuItemImage::create(//定义推出菜单 "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); this->addChild(pMenu, 1); CCLabelTTF* pLabel = CCLabelTTF::create("Hello World111", "Arial", 24);//定义label CC_BREAK_IF(! pLabel); CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition(ccp(size.width / 2, size.height - 50)); this->addChild(pLabel, 1); CCSprite* pSprite = CCSprite::create("HelloWorld.png"); CC_BREAK_IF(! pSprite); pSprite->setPosition(ccp(size.width/2, size.height/2)); this->addChild(pSprite, 0);
this->setTouchEnabled(true);//设置监听 bRet = true; } while (0); return bRet; } void HelloWorld::menuCloseCallback(CCObject* pSender) { CCDirector::sharedDirector()->end(); }
相关文章推荐
- 一文概览图卷积网络基本结构和最新进展(附视频&代码)
- java数据结构之树基本概念解析及代码示例
- 关于移动网站网页的基本代码结构和一些移动网站开发建议
- C开发lua模块(一) --- 虚拟栈和基本代码结构
- nginx代码分析-基本结构-单链表ngx_list_t
- javascrit开发的基本代码结构的
- JVM学习笔记(1、 基本结构;2、Java代码编译和执行的整个过程3、内存管理和垃圾回收 4、 内存调优 )
- MySQL 代码结构与基本流程
- 中介者模式分析、结构图及基本代码
- 适配器模式的分析、结构图及基本代码
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 模板方法模式分析、结构图和基本代码
- 职责链模式分析、结构图与基本代码
- 关于移动网站网页的基本代码结构和一些移动网站开发建议
- 解释器模式分析、结构图及基本代码
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 观察者模式分析、结构图及基本代码
- nginx代码分析-基本结构-动态数组ngx_array_t
- 迭代器模式分析、结构图及基本代码
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果