Lua 常用方法

来源:互联网 发布:淘宝店企业和个人区别 编辑:程序博客网 时间:2024/06/15 19:55

Lua-cocos2d-x
0

作者:Burning_Man更新于 05月24日访问(406评论(1

结束程序

CCDirector:sharedDirector():endToLua()

场景转换

CCDirector:sharedDirector():replaceScene(/scene/)

清除缓存

CCDirector:sharedDirector():purgeCachedData()
一般在新场景生成的时候调用,清除掉的缓存在再次使用的时候还是会加载进来

CCLayer touch事件监听

/cc_layer_obj/:setTouchEnabled(true) => 设置此layer监听touch
/cc_layer_obj/:registerScriptTouchHandler(/int function_point/) => 设置touch回调函数
/cc_layer_obj/:unregisterScriptTouchHandler(/int function_point/) => 移除回调监听
touch回调写法:
eventType => "began" or "moved" or "ended" or "cancelled"
local function on_touch(eventType, x, y)
end

CCLayer生命周期监听

继承CCLayer的类实现以下函数
function /class/:onEnter() end => 载入
function /class/:onEnterTransitionDidFinish() end => 使用CCTransitionScene在Transition完成时调用
function /class/:onExit() end => 退出
CCTransitionScene使用时的情况

CCLayer keypad事件监听

/cc_layer_obj/:setKeypadEnabled(true) => 设置此layer监听keypad
/cc_layer_obj/:registerScriptKeypadHandler(/int function_p/) => 设置keypad回调函数
/cc_layer_obj/.unregisterScriptKeypadHandler(/int function_p/) => 移除回调监听
keypad回调写法:
key => "backClicked" or "menuClicked"
local function on_keypad_pressed(key)
end

指定第一个运行的Lua文件

在AppDelegate.cpp中
CCString* pstrFileContent = CCString::createWithContentsOfFile(/xxx.lua/);

Lua开始程序

-- avaoid memory leak
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 500)

local scene = /xxx_scene/
CCDirector:sharedDirector:runWithScene(scene) => Enters the Director's main loop with the given Scene.
Call it to run only your FIRST scene. Don't call it if there is already a running scene.

CCMenuItem设置点击事件

/cc_menu_item/:registerScriptTapHandler(/call_back/) => 设置点击回调
回调函数写法
tag => 被点击的menuitem的tag
local function call_back(tag)
end

CCScheduler schedule

unsigned int scheduleScriptFunc(unsigned int nHandler, float fInterval, bool bPaused )
返回的int用于unscheduleScriptEntry的时候使用, fInterval调用周期, bPaused设置为false,不知何用
void unscheduleScriptEntry (unsigned int uScheduleScriptEntryID) => unschedule
demo:
local function call_back() print("scheduled") end
entry = CCDirector:getScheduler():scheduleScriptFunc(call_back, 2, false)
CCDirector:getScheduler():unscheduleScriptEntry(entry)

0 0