Coos2d-x网站:http://www.cocos2d-x.org/
Windows下开发,所以下载源码解压后先用build-win32.bat编译可执行文件,编译完后在HelloLua或者multi-platform-lua工程中修改一下C++源代码,不改也可以。
main.c:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); #ifdef USE_WIN32_CONSOLE AllocConsole(); freopen("CONIN$", "r", stdin); freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); #endif // create the application instance AppDelegate app; CCUserDefault* userDefault = CCUserDefault::sharedUserDefault(); //用于从XML文件读取用户配置 std::string viewName = userDefault->getStringForKey("viewName", "WinApp"); float viewWidth = userDefault->getFloatForKey("viewWidth", 480); float viewHeight = userDefault->getFloatForKey("viewHeight", 320); float zoomFactor = userDefault->getFloatForKey("zoomFactor", 1); CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setViewName(viewName.c_str()); eglView->setFrameSize(viewWidth, viewHeight); eglView->setFrameZoomFactor(zoomFactor); int ret = CCApplication::sharedApplication()->run(); #ifdef USE_WIN32_CONSOLE FreeConsole(); #endif return ret; }
修改了一些地方,将一些配置从xml文件中读取,免得以后对要他们修改还要重新编译:
CCUserDefault* userDefault = CCUserDefault::sharedUserDefault(); //用于从XML文件读取用户配置 std::string viewName = userDefault->getStringForKey("viewName", "WinApp"); float viewWidth = userDefault->getFloatForKey("viewWidth", 480); float viewHeight = userDefault->getFloatForKey("viewHeight", 320); float zoomFactor = userDefault->getFloatForKey("zoomFactor", 1); CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setViewName(viewName.c_str()); eglView->setFrameSize(viewWidth, viewHeight); eglView->setFrameZoomFactor(zoomFactor);
CCUserDefault会利用tinyxml2读写xml文件,从而保存或者读取我们所需要的基本类型数据。
这样也避免了转码操作。
在AppDelegate初始化时加载Lua文件,主要就改了一下路径:
std::string searchPath = _userDefault->getStringForKey("searchPath", "script"); std::string scriptName = _userDefault->getStringForKey("scriptName", "main.lua"); //std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("hello.lua"); CCFileUtils::sharedFileUtils()->addSearchPath(searchPath.c_str()); pEngine->executeScriptFile(scriptName.c_str());
这样启动时就去执行script/main.lua
编译后就不用管C++代码了,以后的lua代码修改后也不用再编译。
现在有了可执行文件,拷到没装VisualStudio的PC上也可以开发cocos了
下面写lua代码:
local function main() -- avoid memory leak collectgarbage("setpause", 100) collectgarbage("setstepmul", 5000) print(os.date()) --打印系统时间 local scene = CCScene:create() local layer = CCLayer:create() layer:setTouchEnabled(true) layer:registerScriptTouchHandler(function(eventType, x, y) --响应屏幕触摸事件 if eventType == "began" then print(eventType, x, y) return true elseif eventType == "ended" then print(eventType, x, y) end end) scene:addChild(layer) CCDirector:sharedDirector():setDisplayStats(true); CCDirector:sharedDirector():runWithScene(scene) end main()
debug程序通过print打印
参照LuaTest代码写一个小电子时钟程序
local function main() -- avoid memory leak collectgarbage("setpause", 100) collectgarbage("setstepmul", 5000) --获取定时任务队列 local scheduler = CCDirector:sharedDirector():getScheduler() --获取屏幕尺寸 local winSize = CCDirector:sharedDirector():getWinSize() --创建layer local layer = CCLayer:create() --获取位图字体 local bmpFontTime = CCLabelBMFont:create(os.date('%X'), "bitmapFontTest3.fnt") local bmpFontDate = CCLabelBMFont:create(os.date('%x'), "bitmapFontTest3.fnt") local bmpFontFast = CCLabelBMFont:create('0', "bitmapFontTest3.fnt") --设置坐标 bmpFontTime:setPosition(winSize.width / 2, winSize.height / 2) bmpFontDate:setPosition(winSize.width / 2, winSize.height / 2 + 48) bmpFontFast:setPosition(winSize.width / 2, winSize.height / 2 - 48) --添加到layer layer:addChild(bmpFontTime) layer:addChild(bmpFontDate) layer:addChild(bmpFontFast) --定时器响应函数 local function secondInterval(dt) print(dt, os.date('%X')) bmpFontTime:setString(os.date('%X')) bmpFontDate:setString(os.date('%x')) end --定时器响应函数 local interval = 0 local function fastInterval(dt) interval = interval+1 print(interval) bmpFontFast:setString(interval) end --设置定时任务 local schedulerEntry1 = nil local schedulerEntry2 = nil layer:registerScriptHandler(function(event) if event == "enter" then print('schedule enter') schedulerEntry1 = scheduler:scheduleScriptFunc(secondInterval, 1.0, false) schedulerEntry2 = scheduler:scheduleScriptFunc(fastInterval, 0.1, false) elseif event== "exit" then print('schedule exit') scheduler:unscheduleScriptEntry(schedulerEntry1) scheduler:unscheduleScriptEntry(schedulerEntry2) if CCDirector:sharedDirector():isPaused() then CCDirector:sharedDirector():resume() end end end) --响应屏幕触摸事件 layer:setTouchEnabled(true) layer:registerScriptTouchHandler(function(eventType, x, y) if eventType == "began" then print(eventType, x, y) return true elseif eventType == "ended" then print(eventType, x, y) CCDirector:sharedDirector():setDisplayStats(not CCDirector:sharedDirector():isDisplayStats()); end end) local scene = CCScene:create() scene:addChild(layer) CCDirector:sharedDirector():runWithScene(scene) end main()
新手。
相关推荐
Cocos2dx Lua开发的核心技术。本书从Lua语言基础开始,全面介绍了Cocos2dx Lua的基础知识、基本应用和高级编程技术,并最终介绍了游戏的实战开发。全书贯穿理论结合实践的编写方式,各个章节都提供了配套实例。
Cocos2dx Lua开发的核心技术。本书从Lua语言基础开始,全面介绍了Cocos2dx Lua的基础知识、基本应用和高级编程技术,并最终介绍了游戏的实战开发。全书贯穿理论结合实践的编写方式,各个章节都提供了配套实例。
本图书为Cocos2d-x Luad 基础知识,基本应用和高级编程技术,欢迎大家下载学习!
quick-cocos2d-x是cocos2d-x开发商“”专门针对Lua开发者推出的扩展版本。 Quick-Cocos2d-x基于Cocos2d-x开发,具有Cocos2d-x的所有优点。并在Cocos2d-x基础上添加了Lua脚本支持,和高级脚本框架。开发效率,更省的...
第2章 Cocos2D-X引擎的 开发环境 21 2.1 跨平台的开发 21 2.2 建立开发环境 23 2.2.1 PC开发环境 23 2.2.2 Android开发环境 26 2.2.3 iOS开发环境 35 2.3 引擎中的混合编译 38 2.3.1 Java与C++的混合编译 38 2.3.2 ...
钟迪龙所著的《Cocos2d-x游戏开发之旅(附光盘)》介绍Cocos2d-x的基础知识:基本知识、基本结构、控件、动作以及屏幕触摸事件的使用。介绍Cocos2d-x更高阶的内容,包括渲染效率的提高、动画、TexturePacker图片打包、...
第13~15 章讲解Cocos2d-x 中的Lua 脚本语言开发、CocosStudio 工具及粒子系统的使用方法;第16~19 章讲解数据统计工具的使用方法、项目跨平台移植发布、iOS 真机测试与发布、SVN 版本控制;第20 章讲解一个完整的...
第四章主要介绍Cocos2d-x的环境搭建和HelloWorld的项目实现,通过本章读者可以快速上手开发一个最简单的移动游戏UI,并通过该案例以了解Cocos2d-x的运行原理。 第五章主要介绍Cocos2d-x核心技术,这里包括Cocos...
Lua 社区版是在Cocos2d-x的基础上,关注于Lua进行游戏开发的引擎框架变种,意在减少重复造轮子,节省开发人员的时间。
对于我来说,Cocos2d-x就是一个类库,就类似于MFC、ATL和QT一样,就是一个游戏类库,而我就按照学习类库使用的方法去学习Cocos2d-x。这篇文章叫基础概念,但是还是从整体来说说Cocos2d-x这个类库的。 基础架构 在...
Cocos2d-x Lua社区版是在Cocos2d-x的基础上,关注于Lua进行游戏开发的引擎框架变种,意在减少重复造轮,节省开发人员的时间。 官方网站 社区版千人群(1群)号:361920466 社区版2群:138934064 相关开源仓库地址 ...
详细说明见: https://blog.csdn.net/Tiantangbujimo7/article/details/124861145?spm=1001.2014.3001.5502