require
"hello2"
-- 包含hello2这个脚本
-- 注视语句
-- 基本上调用的cocos2dx函数和类的时候就是以cocos2d.*这样子来用
-- 注意2:function 关键字定义函数,end结束函数
-- 打印
cocos2d.CCLuaLog(
"脚本hello开始运行... "
.. myadd(3,5))
-- 创建一个Scene
sceneForWorld = cocos2d.CCScene:node()
-- 创建一个Layer
layerForWorld = cocos2d.CCLayer:node()
sceneForWorld:addChild(layerForWorld)
-- 创建一个精灵
spriteForWorld = cocos2d.CCSprite:spriteWithFile(
"Icon.png"
)
layerForWorld:addChild(spriteForWorld)
-- 获取屏幕宽高
screenSize=cocos2d.CCDirector:sharedDirector():getWinSize()
-- 设置精灵坐标
spriteForWorld:setPosition(cocos2d.CCPoint(screenSize.width*0.5,screenSize.height*0.5))
-- 设置精灵缩放2倍
spriteForWorld:setScale(2)
-- 添加一个CCLabelTTF (!!!!!!备注!!!!!!)
myLableTTF =cocos2d.CCLabelTTF:labelWithString(
"Himi- Lua 基础"
,
"Helvetica-Bold"
myLableTTF:setPosition(cocos2d.CCPoint(screenSize.width*0.5,screenSize.height*0.5+100))
sceneForWorld:addChild(myLableTTF)
-- 添加一个CCLabelTTF
myLableTTF2 =cocos2d.CCLabelTTF:labelWithString(
"上面icon跟随用户触屏位置"
myLableTTF2:setPosition(cocos2d.CCPoint(screenSize.width*0.5,screenSize.height*0.5-100))
sceneForWorld:addChild(myLableTTF2)
-- @@@@@@@@@@触摸事件
--开启触摸
layerForWorld:setIsTouchEnabled(
true
)
-- 注册触摸事件
layerForWorld.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN,Fixed; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; min-height:inherit!important; color:blue!important">"btnTouchBegin"
)
layerForWorld.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED,Fixed; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; min-height:inherit!important; color:blue!important">"btnTouchMove"
)
layerForWorld.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED,Fixed; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; min-height:inherit!important; color:blue!important">"btnTouchEnd"
)
-- touch handers
pointBegin = nil
function btnTouchBegin(e)
)
local v = e[1]
local pointMove = v:locationInView(v:view())
pointMove = cocos2d.CCDirector:sharedDirector():convertToGL(pointMove)
spriteForWorld:setPosition(cocos2d.CCPoint(pointMove.x,pointMove.y))
end
function btnTouchMove(e)
)
local v = e[1]
local pointMove = v:locationInView(v:view())
pointMove = cocos2d.CCDirector:sharedDirector():convertToGL(pointMove)
end
function btnTouchEnd(e)
)
end
-- @@@@@@@@@@触摸结束
--动态小狗
winSize = cocos2d.CCDirector:sharedDirector():getWinSize()
FrameWidth = 105
FrameHeight = 95
textureDog = cocos2d.CCTextureCache:sharedTextureCache():addImage(
"dog.png"
)
frame0 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog,cocos2d.CCRectMake(0,FrameWidth,FrameHeight))
frame1 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog,cocos2d.CCRectMake(FrameWidth*1,FrameHeight))
spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0)
spriteDog:setPosition(cocos2d.CCPoint(100,winSize.height/4*3))
layerForWorld:addChild(spriteDog)
animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:
new
(2)
animFrames:addObject(frame0)
animFrames:addObject(frame1)
animation = cocos2d.CCAnimation:animationWithFrames(animFrames,0.5)
animate = cocos2d.CCAnimate:actionWithAnimation(animation,
false
);
spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate))
--自定义函数
function prForHimi()
"reFresh function"
)
--取消选择器
--cocos2d.CCScheduler:sharedScheduler():unscheduleScriptFunc(
"prForHimi"
)
end
--使用选择器进行函数更新
--cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc(
)
--循环语句
for
i=0,4,1
do
j=0,2
do
"for loop"
end
end
-- 避免内存泄漏
collectgarbage(
"setpause"
"setstepmul"
-- 播放背景音乐
--CocosDenshion.SimpleAudioEngine:sharedEngine():playBackgroundMusic(
"background.mp3"
)
-- 播放音效
--CocosDenshion.SimpleAudioEngine:sharedEngine():preloadEffect(
"effect1.wav"
)
-- run整个scene
cocos2d.CCDirector:sharedDirector():runWithScene(sceneForWorld)
"脚本hello正常执行结束... "
运行效果图如下:
对于Himi上面给出的自己修改后的代码注视写的狠清楚了 =。 = 所以不多加赘述,但是Himi这里需要还要详细说一点;
脚本lua等一般都示通过中间层(解析)进行与前端代码(Cocos2dX封装的引擎类库)交互,所以很多方法名称可能发生了改变,那么对于不太熟悉的童鞋我们如何下手?
OK,如刚才的代码中有个“备注”,不知道细心童鞋们看到没有,这里是添加了一个CCLabelTTF ,假如我们不知道它的构造函数是否有修改,或者说参数忘记都是什么了,那么请打开你项目的libs文件夹,然后打开lua文件夹,继续打开cocos2dx_support文件夹找到 LuaCocos2d.cpp文件打开,(注意这个文件代码很多,打开较慢)然后你会看到很多方法的定义与实现!
那么假如我们来找 CCLabelTTF的构造方法,那么搜一下如下语句:
1
tolua_beginmodule(tolua_S,Fixed; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; min-height:inherit!important; color:blue!important">"CCLabelTTF"
);
|
你将发现此类下方一大批类似的代码:
没错这里就是此类的所有是lua-cocos2dx之间的转换函数定义,比如常用的CCLabelTTF构造函数:
tolua_function(tolua_S,Fixed; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; min-height:inherit!important; color:blue!important">"labelWithString"
此函数第一参数大家不用理会,第二个参数表示我们使用cocos2d/x时调用的函数名称,后面则是lua-cocos2dx之间的转换函数实现代码,大家可以继续搜索第三个参数或者按住command然后点击第三个参数找到其函数实现代码:
28
#ifndef TOLUA_DISABLE_tolua_Cocos2d_cocos2d_CCLabelTTF_labelWithString01
static
int
tolua_Cocos2d_cocos2d_CCLabelTTF_labelWithString01(lua_State* tolua_S)
{
tolua_Error tolua_err;
if
(
!tolua_isusertable(tolua_S,Fixed; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; min-height:inherit!important; color:blue!important">"cocos2d::CCLabelTTF"
!tolua_isstring(tolua_S,2,&tolua_err) ||
!tolua_isnumber(tolua_S,&tolua_err) ||
!tolua_isnoobj(tolua_S,5,&tolua_err)
)
goto
tolua_lerror;
else
{
const
char
* label = ((
*) tolua_tostring(tolua_S,0));
* fontName = ((
float
fontSize = ((
) tolua_tonumber(tolua_S,0));
{
cocos2d::CCLabelTTF* tolua_ret = (cocos2d::CCLabelTTF*) cocos2d::CCLabelTTF::labelWithString(label,fontName,fontSize);
tolua_pushusertype(tolua_S,(
void
*)tolua_ret,Fixed; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; min-height:inherit!important; color:black!important">);
}
}
return
1;
tolua_lerror:
tolua_Cocos2d_cocos2d_CCLabelTTF_labelWithString00(tolua_S);
}
#endif //#ifndef TOLUA_DISABLE
|
在这里看到此函数转换过程,并可以很清楚看到:
到这里大家会很清楚需要的参数都是哪些,如果还不清楚,继续按住Command然后点击labelWithString进入cocos2dx引擎代码CCLabelTTF.cpp中的此函数实现啦!
可能这部分有童鞋看得比较迷茫 =。 = 那么Himi来简化这些复杂来说:
解析lua脚本中的一句代码->通过解析层代码->将其转换并转到前端代码进行使用
那么当然此过程也可逆:
前端代码->通过解析层代码->使用lua脚本中小编
这里由于Himi对cocos2dx 中的lua还没有深入了解,所以不知是否过程可逆;
OK,基本就是这样,对于脚本的熟悉,主要还是在公司进行使用然后慢慢熟悉和熟练掌握的,本章主要需要童鞋们记住的是脚本的概念和作用!
相关文章
本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1. 来源 QuickV3sample项目中的2048样例游戏,以及最近《...
Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...