Cocos2d-x 学习随记之一 hello,world工程注释!!
来源:互联网 发布:假设有二维数组a6*8 编辑:程序博客网 时间:2024/05/16 07:07
空闲的时间学习一下Cocos2d,顺手记录一些逝去的青春。。。。。
代码段如下:主要学习工程的流程以及注释解释。。。
//main.cpp 函数入口
#include "main.h"#include "AppDelegate.h"#include "CCEGLView.h"USING_NS_CC;int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){ UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // 创建应用程序实例 AppDelegate app; CCEGLView* eglView = CCEGLView::sharedOpenGLView();//设置标题栏的名称 eglView->setViewName("mythcpp");//设置窗口大小 eglView->setFrameSize(480, 320); return CCApplication::sharedApplication()->run();}
//main.h头文件,这些都有脚本自动生成
#ifndef __MAIN_H__#define __MAIN_H__#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers// Windows Header Files:#include <windows.h>#include <tchar.h>// C RunTime Header Files #include "CCStdC.h"#endif // __MAIN_H__
//AppDelegate.cpp 主要是导演类的定义以场景的管理,都在这里处理。
#include "AppDelegate.h"#include "HelloWorldScene.h"USING_NS_CC;AppDelegate::AppDelegate() {//AppDelegate构造函数}AppDelegate::~AppDelegate() {//AppDelegate析构函数}// 程序启动后调用此方法bool AppDelegate::applicationDidFinishLaunching() { // 初始化导演类 CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();//设置OpenGL视图 pDirector->setOpenGLView(pEGLView); // 设置是否打开FPS、0或flase为不打开 pDirector->setDisplayStats(true); // 设置FPS默认值,为每秒60帧 pDirector->setAnimationInterval(1.0 / 60); // 创建一个场景,其使用智能指针进行内存管理,引用计数器方式 CCScene *pScene = HelloWorld::scene(); // 运行场景,真正的游戏开始。 pDirector->runWithScene(pScene); return true;}// 当进入后台,运行此方法,void AppDelegate::applicationDidEnterBackground() { CCDirector::sharedDirector()->stopAnimation(); // 如果有声音,请进行声音暂停。 // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();}// 恢复游戏调用此方法。void AppDelegate::applicationWillEnterForeground() { CCDirector::sharedDirector()->startAnimation(); //恢复声音 // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();}
//AppDelegate.h
#ifndef _APP_DELEGATE_H_#define _APP_DELEGATE_H_#include "cocos2d.h"/**@brief The cocos2d Application.The reason for implement as private inheritance is to hide some interface call by CCDirector.*/class AppDelegate : private cocos2d::CCApplication{public: AppDelegate(); virtual ~AppDelegate(); /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. @return false Initialize failed, app terminate. */ virtual bool applicationDidFinishLaunching(); /** @brief The function be called when the application enter background @param the pointer of the application */ virtual void applicationDidEnterBackground(); /** @brief The function be called when the application enter foreground @param the pointer of the application */ virtual void applicationWillEnterForeground();};#endif // _APP_DELEGATE_H_
//HelloWorldScene.cpp helloWorld场景的实现
#include "HelloWorldScene.h"using namespace boost;USING_NS_CC;CCScene* HelloWorld::scene(){ //‘场景’是一个自动释放的对象 CCScene *scene = CCScene::create(); // 'layer' 是一个自动释放的对象 HelloWorld *layer = HelloWorld::create(); // 添加 图层 到场景 scene->addChild(layer); // return the scene//返回场景 return scene;}// 在这里进行场景的一系列的初始化bool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 2. 以“X”的形象,这是点击退出程序添加一个菜单项 //你可以修改它。 // 添加一个退出关闭按钮,它是一个自动释放的对象 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); ///////////////////////////// // 3. 添加你的代码.... //添加一个标签以显示hello world // 创建并初进行始化标签 //CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); CCLabelTTF* pLabel = CCLabelTTF::create("Hello World!!!", "Arial", 48); // 在场景中心位置显示标签 pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // 添加标签到这一层 this->addChild(pLabel, 1); // 创建hello world背景图 CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // 在场景中心位置显示 pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); //添加helloWorld精灵到这一层 this->addChild(pSprite, 0); return true;}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}
//"HelloWorldScene.h"
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "boost_head.h"class HelloWorld : public cocos2d::CCLayer{public: //初始化声明virtual bool init(); // 返回实例的指针 static cocos2d::CCScene* scene(); // 一个选择回调函数 void menuCloseCallback(CCObject* pSender); // 内存回收 CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
0 0
- Cocos2d-x 学习随记之一 hello,world工程注释!!
- cocos2d-2.1rc0-x-2.1.3建android工程Hello World例子
- Cocos2d-x--"Hello World"深入分析
- cocos2d-x教程1 hello world
- cocos2d-x之浅析Hello World
- cocos2d-x-3.3rc2-001-hello-world
- 【Cocos2d-x 游戏开发】 1 --- Hello World
- cocos2d-x 的安装以及hello world
- cocos2d-x学习之一
- 【cocos2d-x】cocos2d-x学习笔记之一
- python学习之一始于hello world
- Struts2框架学习之一:Hello World程序
- Hibernate框架学习之一:Hello World程序
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第五课:Hello World
- Cocos2d-x学习之一 坐标系
- Cocos2d-x学习之一 坐标系
- Cocos2d-x学习之一 坐标系
- Cocos2d-x随记
- 2014-2-18-WebView 调用javascript 获取网页内容
- U盘乱码修复方法
- 实模式,保护模式,虚拟保护模式
- JDBC数据类型
- Hadoop到底是什么?
- Cocos2d-x 学习随记之一 hello,world工程注释!!
- 哭累了,沉默了 爱情失恋 受伤励志 人生青春
- IOS中定时器NSTimer的开启与关闭
- C#日志封装
- jsatck pid 中 各种情况解释
- 如何查看Memcache 运行状态 stats(Status) —— Memcache Telnet 接口
- PHP与JAVA使用AES128位加密通信
- Discuz X3插件开发基础教程
- GPRS模块(MG2639)TEXT模式和PDU模式发送短消息详解(含代码片段)