里面自带的C++代码很简单。
但是里面有一句:
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("hello.lua");
pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
pEngine->executeScriptFile(path.c_str());
就是从文件读lua文件bing执行。
好嘛,我们看下这个hello.lua代码是怎么样的:
(PS:我注释了一处非常滑稽的代码)
-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
print("----------------------------------------")
print("LUA ERROR: " .. tostring(msg) .. "\n")
print(debug.traceback())
print("----------------------------------------")
end
function myadd(x, y)
return x + y
end
local function main()
-- avoid memory leak
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
local cclog = function(...)
print(string.format(...))
end
--require "hello2"
--cclog("result is " .. myadd(3, 5))
---------------
local visibleSize = CCDirector:sharedDirector():getVisibleSize()
local origin = CCDirector:sharedDirector():getVisibleOrigin()
-- add the moving dog
local function creatDog()
local frameWidth = 105
local frameHeight = 95
-- create dog animate
local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png")
local rect = CCRectMake(0, 0, frameWidth, frameHeight)
local frame0 = CCSpriteFrame:createWithTexture(textureDog, rect)
rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight)
local frame1 = CCSpriteFrame:createWithTexture(textureDog, rect)
local spriteDog = CCSprite:createWithSpriteFrame(frame0)
spriteDog.isPaused = false
spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3)
local animFrames = CCArray:create()
animFrames:addObject(frame0)
animFrames:addObject(frame1)
local animation = CCAnimation:createWithSpriteFrames(animFrames, 0.5)
local animate = CCAnimate:create(animation);
spriteDog:runAction(CCRepeatForever:create(animate))
-- moving dog at every frame
local function tick()
if spriteDog.isPaused then return end
local x, y = spriteDog:getPosition()
if x > origin.x + visibleSize.width then
x = origin.x
else
x = x + 1
end
spriteDog:setPositionX(x)
end
CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(tick, 0, false)
return spriteDog
end
-- create farm
local function createLayerFarm()
local layerFarm = CCLayer:create()
-- add in farm background
local bg = CCSprite:create("farm.jpg")
bg:setPosition(origin.x + visibleSize.width / 2 + 80, origin.y + visibleSize.height / 2)
layerFarm:addChild(bg)
-- add land sprite
for i = 0, 3 do
for j = 0, 1 do
local spriteLand = CCSprite:create("land.png")
spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)
layerFarm:addChild(spriteLand)
end
end
-- add crop
local frameCrop = CCSpriteFrame:create("crop.png", CCRectMake(0, 0, 105, 95))
for i = 0, 3 do
for j = 0, 1 do
local spriteCrop = CCSprite:createWithSpriteFrame(frameCrop);
spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2)
layerFarm:addChild(spriteCrop)
end
end
-- add moving dog
local spriteDog = creatDog()
layerFarm:addChild(spriteDog)
-- handing touch events
local touchBeginPoint = nil
local function onTouchBegan(x, y)
cclog("onTouchBegan: %0.2f, %0.2f", x, y)
touchBeginPoint = {x = x, y = y}
spriteDog.isPaused = true
-- CCTOUCHBEGAN event must return true
return true
end
local function onTouchMoved(x, y)
cclog("onTouchMoved: %0.2f, %0.2f", x, y)
if touchBeginPoint then
local cx, cy = layerFarm:getPosition()
layerFarm:setPosition(cx + x - touchBeginPoint.x,
cy + y - touchBeginPoint.y)
touchBeginPoint = {x = x, y = y}
end
end
local function onTouchEnded(x, y)
cclog("onTouchEnded: %0.2f, %0.2f", x, y)
touchBeginPoint = nil
spriteDog.isPaused = false
end
local function onTouch(eventType, x, y)
if eventType == "began" then
return onTouchBegan(x, y)
elseif eventType == "moved" then
return onTouchMoved(x, y)
else
return onTouchEnded(x, y)
end
end
layerFarm:registerScriptTouchHandler(onTouch)
layerFarm:setTouchEnabled(true)
return layerFarm
end
-- create menu
local function createLayerMenu()
local layerMenu = CCLayer:create()
local menuPopup, menuTools, effectID
local function menuCallbackClosePopup()
-- stop test sound effect
SimpleAudioEngine:sharedEngine():stopEffect(effectID)
menuPopup:setVisible(false)
end
local function menuCallbackOpenPopup()
-- loop test sound effect
local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav")
effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath)
menuPopup:setVisible(true)
end
-- add a popup menu
local menuPopupItem = CCMenuItemImage:create("menu2.png", "menu2.png")
menuPopupItem:setPosition(0, 0)
menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup)
menuPopup = CCMenu:createWithItem(menuPopupItem)
menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)
menuPopup:setVisible(false)
layerMenu:addChild(menuPopup)
-- add the left-bottom "tools" menu to invoke menuPopup
local menuToolsItem = CCMenuItemImage:create("menu1.png", "menu1.png")
menuToolsItem:setPosition(0, 0)
menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup)
menuTools = CCMenu:createWithItem(menuToolsItem)
local itemWidth = menuToolsItem:getContentSize().width
local itemHeight = menuToolsItem:getContentSize().height
menuTools:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2)
layerMenu:addChild(menuTools)
return layerMenu
end
-- play background music, preload effect
-- uncomment below for the BlackBerry version
-- local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.ogg")
local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.mp3")
SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true)
local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav")
SimpleAudioEngine:sharedEngine():preloadEffect(effectPath)
-- run
local sceneGame = CCScene:create()
sceneGame:addChild(createLayerFarm())
sceneGame:addChild(createLayerMenu())
CCDirector:sharedDirector():runWithScene(sceneGame)
end
xpcall(main, __G__TRACKBACK__)
那么我看看最上面提到的c++代码是如何调用lua的:
pEngine->executeScriptFile(path.c_str());
然后调用m_stack->executeScriptFile(filename);
这个m_strack是神马玩意呢?
其实在生成strack做了一些初始化工作:
m_state = lua_open();
luaL_openlibs(m_state);
tolua_Cocos2d_open(m_state);
toluafix_open(m_state);
啊啊,里面还用到了tolua库,就是把C++代码转成lua。关于tolua可以参考http://www.codenix.com/~tolua/tolua++.html
toluais a tool that greatly simplifies the integration of C/C++ code withLua.
Based on acleanedheader file(or
extracts from real header files),toluaautomatically generates the binding code to access C/C++ features from Lua. Using Lua API and tag method
facilities,toluamaps C/C++ constants, external variables, functions, classes, and methods to Lua.
关于cocod-2x里面的关于tolua可以直接看源码,有注释。
主要就是tolua_Cocos2d_open里完成了一些重要工作:
举个例子,上面的lua调用到CCTextureCache:sharedTextureCache():addImage("dog.png"),然后这里面就写得有:
比如将一个类加到luastate就可以这样:
tolua_beginmodule(tolua_S,"CCTextureCache");
tolua_function(tolua_S,"addImage",tolua_Cocos2d_CCTextureCache_addImage00);
tolua_function(tolua_S,"sharedTextureCache",tolua_Cocos2d_CCTextureCache_sharedTextureCache00);
tolua_endmodule(tolua_S);
这样就知道,为毛这个hello.lua可以这么轻松地调用coco2d-x里的API了。
分享到:
相关推荐
这个名为“coco2d-x基础资料-doc”的压缩包文件显然包含了关于Cocos2d-x的基础学习材料和文档。以下是对Cocos2d-x及其相关知识点的详细解释。 一、Cocos2d-x概述 Cocos2d-x是一个跨平台的游戏开发框架,它是Cocos2d...
这是我重新弄的cocos2d-x-3.0的类图.之前别人兄台弄的,有些不全面,有些地方错误.我这个可以说是最新的了.每个类添加了中文的详细注解,同时也添加了中文的类名称翻译.这样对cocos2d-x-3.0的框架比较好上手. 有兴趣的...
总的来说,`UIView`和Coco2d-x的场景切换是iOS游戏开发中常见的技术挑战,它要求开发者对两者都有深入的理解,以便能够流畅地在原生UI和游戏场景之间导航。这种切换不仅涉及视图的管理,还涉及到数据传递和状态同步...
Cocos2d-x是一款开源的游戏开发框架,广泛用于2D游戏、交互式应用程序和教育软件的制作。2.x版本是其历史上的一个重要阶段,提供了丰富的功能和改进。在这个"cocos2d-x 2.x action集合(详细注释分类)"的资源中,你将...
coco2d-x2.03 chm 文档.
《Flappy Bird基于Cocos2d-x 3.8的游戏开发详解》 Flappy Bird是一款在移动设备上风靡一时的简单却极具挑战性的游戏,它的成功在于其极简的设计和难以掌握的游戏机制。本教程将详细介绍如何使用Cocos2d-x 3.8框架来...
Coco2d-js是Cocos2d家族的一员,该家族还包括Cocos2d-x等其他语言版本。Coco2d-js具有跨平台特性,可以用来开发iOS、Android、Windows等多平台的应用程序。其主要特点包括: 1. **高性能**:利用WebGL技术进行图形...
《BulletWar游戏源码解析——基于Cocos2d-x与Lua的开发实践》 在游戏开发领域,Cocos2d-x是一款广泛使用的2D游戏引擎,尤其受到独立开发者和小型团队的青睐。本项目名为“BulletWar”,是利用Cocos2d-x 3.6.1版本,...
《cocos2d-x3.2贪吃蛇游戏代码与资源》是一款基于cocos2d-x3.2引擎开发的经典游戏——贪吃蛇。cocos2d-x是一个强大的开源跨平台2D游戏开发框架,使用C++语言编写,支持iOS、Android、Windows等多个操作系统。在这款...
在《Cocos2d-x 3.x制作2048》这本书中,作者详细介绍了使用Cocos2d-x 3.0版本,通过C++语言开发2048游戏的全过程。2048是一款非常流行的数字拼接游戏,玩家需要通过滑动屏幕上下左右来移动数字卡片,相同数字的卡片...
《cocos2d-x API详解:打造高效游戏开发利器》 cocos2d-x是一款开源的游戏开发框架,专为跨平台游戏开发设计,支持iOS、Android、Windows等操作系统。其API(应用程序接口)是开发者与引擎交互的核心,通过熟练掌握...
基于coco2d-x引擎,用C++语言,自己写的一个flappy bird。
这个“coco2d-X 游戏资源合集”显然是一份针对初学者的学习材料,旨在帮助新手快速上手游戏开发。 首先,让我们了解一下Cocos2d-X的核心组件和功能: 1. **渲染引擎**:Cocos2d-X使用OpenGL ES进行图形渲染,支持...
【cocos2d-x游戏引擎概述】 cocos2d-x是一个开源的游戏开发框架,它基于cocos2d-x,用于创建2D游戏、演示程序和其他图形/交互式应用程序。cocos2d-x是用C++编写的,但同时也支持Lua和JavaScript作为脚本语言,使得...
《cocos2d-x 3.x 游戏开发实战资源》是针对使用cocos2d-x 3.x框架进行游戏开发的学习者所准备的一套实战资料。cocos2d-x是一个开源的游戏开发框架,它基于cocos2d,并且支持跨平台开发,包括iOS、Android、Windows等...
《cocos2d-x 3.8 坦克大战》是一个使用cocos2d-x游戏引擎开发的经典游戏重制项目。cocos2d-x是一个跨平台的2D游戏开发框架,广泛应用于iOS、Android、Windows等多平台游戏开发。在这款坦克大战中,开发者利用cocos2d...
ActionTimeline动画的使用,二、修改动画,关键帧事件,创建多个Node对象,异步加载动画Armature,创建动画并显示,设置骨骼动画的速度,批量添加怪物,实现动画的事件...在场景中获取粒子效果等等COCOS2D-X使用教程尽在其中
《我所理解的Cocos2d-x》针对最新的 Cocos2d-x 3.x版本,介绍了Coco2d-x游戏引擎的基本架构、渲染机制,以及各个子模块的功能和原理,并结合OpenGL ES图形渲染管线,深入探讨了游戏开发中涉及的相关图形学的知识,...
《cocos2d-x与Box2D结合使用详解——基于PhysicsEditor的debugdraw实例》 在游戏开发领域,cocos2d-x是一个广泛使用的2D游戏引擎,它提供了丰富的功能和高效的性能。Box2D则是一款强大的物理模拟库,常用于实现游戏...
【cocos2d-x酷跑完整源代码】是一款基于cocos2d-x框架开发的跑酷游戏项目,它包含了从游戏设计到实现的所有源代码,适用于开发者进行学习和参考。cocos2d-x是一个跨平台的游戏开发框架,支持iOS、Android、Windows等...