从下面代码可以看出
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename((dirPath + "/controller.lua").c_str());
pEngine->addSearchPath(path.substr(0, path.find_last_of("/") - dirPath.length()).c_str());
pEngine->executeScriptFile(path.c_str());
要从controller.lua里执行lua代码。
那下面来看看里面是些什么:
-- avoid memory leak
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
require "luaScript/mainMenu"
----------------
-- run
local scene = CCScene:create()
scene:addChild(CreateTestMenu())
CCDirector:sharedDirector():runWithScene(scene)
luaScript/mainMenu"
这里又加载了mainMenu.lua,下面再来看看它里面是什么:
require "luaScript/tests"
require "luaScript/helper"
require "luaScript/testResource"
------------------------
local LINE_SPACE = 40
local CurPos = {x = 0, y = 0}
local BeginPos = {x = 0, y = 0}
-- create scene
local function CreateTestScene(nIdx)
CCDirector:sharedDirector():purgeCachedData()
local scene = nil
if nIdx == Test_Table.TEST_ACTIONS then
scene = ActionsTest()
elseif nIdx == Test_Table.TEST_TRANSITIONS then
scene = TransitionsTest()
elseif nIdx == Test_Table.TEST_PROGRESS_ACTIONS then
scene = ProgressActionsTest()
elseif nIdx == Test_Table.TEST_EFFECTS then
scene = EffectsTest()
elseif nIdx == Test_Table.TEST_CLICK_AND_MOVE then
scene = ClickAndMoveTest()
elseif nIdx == Test_Table.TEST_ROTATE_WORLD then
scene = RotateWorldTest()
elseif nIdx == Test_Table.TEST_PARTICLE then
scene = ParticleTest()
elseif nIdx == Test_Table.TEST_EASE_ACTIONS then
scene = EaseActionsTest()
elseif nIdx == Test_Table.TEST_MOTION_STREAK then
scene = MotionStreakTest()
elseif nIdx == Test_Table.TEST_DRAW_PRIMITIVES then
scene = DrawPrimitivesTest()
elseif nIdx == Test_Table.TEST_COCOSNODE then
scene = CocosNodeTest()
elseif nIdx == Test_Table.TEST_TOUCHES then
scene = TouchesTest()
elseif nIdx == Test_Table.TEST_MENU then
scene = MenuTest()
elseif nIdx == Test_Table.TEST_ACTION_MANAGER then
scene = ActionManagerTest()
elseif nIdx == Test_Table.TEST_LAYER then
scene = LayerTest()
elseif nIdx == Test_Table.TEST_SCENE then
scene = SceneTest()
elseif nIdx == Test_Table.TEST_PARALLAX then
scene = ParallaxTest()
elseif nIdx == Test_Table.TEST_TILE_MAP then
scene = TileMapTest()
elseif nIdx == Test_Table.TEST_INTERVAL then
scene = IntervalTest()
elseif nIdx == Test_Table.TEST_CHIPMUNKACCELTOUCH then
--#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
-- pScene = new ChipmunkAccelTouchTestScene()
--#else
--#ifdef MARMALADEUSECHIPMUNK
--#if (MARMALADEUSECHIPMUNK == 1)
-- pScene = new ChipmunkAccelTouchTestScene();
--#endif
-- break;
--#endif
--#endif
elseif nIdx == Test_Table.TEST_LABEL then
elseif nIdx == Test_Table.TEST_TEXT_INPUT then
elseif nIdx == Test_Table.TEST_SPRITE then
scene = SpriteTest()
elseif nIdx == Test_Table.TEST_SCHEDULER then
elseif nIdx == Test_Table.TEST_RENDERTEXTURE then
elseif nIdx == Test_Table.TEST_TEXTURE2D then
elseif nIdx == Test_Table.TEST_BOX2D then
elseif nIdx == Test_Table.TEST_BOX2DBED then
elseif nIdx == Test_Table.TEST_EFFECT_ADVANCE then
elseif nIdx == Test_Table.TEST_ACCELEROMRTER then
--#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
-- elseif nIdx == Test_Table.TEST_KEYPAD then
-- pScene = new KeypadTestScene()
--#endif
elseif nIdx == Test_Table.TEST_COCOSDENSHION then
elseif nIdx == Test_Table.TEST_PERFORMANCE then
scene = PerformanceTest()
elseif nIdx == Test_Table.TEST_ZWOPTEX then
--#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
-- bada don't support libcurl
--#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
-- elseif nIdx == Test_Table.TEST_CURL then
--#endif
--#endif
elseif nIdx == Test_Table.TEST_USERDEFAULT then
elseif nIdx == Test_Table.TEST_BUGS then
elseif nIdx == Test_Table.TEST_FONTS then
elseif nIdx == Test_Table.TEST_CURRENT_LANGUAGE then
--#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
-- elseif nIdx == Test_Table.TEST_TEXTURECACHE then pScene = new TextureCacheTestScene()
--#endif
elseif nIdx == Test_Table.TEST_EXTENSIONS then
elseif nIdx == Test_Table.TEST_SHADER then
elseif nIdx == Test_Table.TEST_MUTITOUCH then
end
return scene
end
-- create menu
function CreateTestMenu()
local menuLayer = CCLayer:create()
local function closeCallback()
CCDirector:sharedDirector():endToLua()
end
local function menuCallback(tag)
print(tag)
local Idx = tag - 10000
local testScene = CreateTestScene(Idx)
if testScene then
CCDirector:sharedDirector():replaceScene(testScene)
end
end
-- add close menu
local s = CCDirector:sharedDirector():getWinSize()
local CloseItem = CCMenuItemImage:create(s_pPathClose, s_pPathClose)
CloseItem:registerScriptTapHandler(closeCallback)
CloseItem:setPosition(ccp(s.width - 30, s.height - 30))
local CloseMenu = CCMenu:create()
CloseMenu:setPosition(0, 0)
CloseMenu:addChild(CloseItem)
menuLayer:addChild(CloseMenu)
-- add menu items for tests
local MainMenu = CCMenu:create()
for index, labelName in pairs(Test_Name) do
local testLabel = CCLabelTTF:create(labelName, "Arial", 24)
local testMenuItem = CCMenuItemLabel:create(testLabel)
testMenuItem:registerScriptTapHandler(menuCallback)
testMenuItem:setPosition(ccp(s.width / 2, (s.height - (index + 1) * LINE_SPACE)))
MainMenu:addChild(testMenuItem, index + 10000, index + 10000)
end
MainMenu:setContentSize(CCSizeMake(s.width, (Test_Table.TESTS_COUNT + 1) * (LINE_SPACE)))
MainMenu:setPosition(CurPos.x, CurPos.y)
menuLayer:addChild(MainMenu)
-- handling touch events
local function onTouchBegan(x, y)
BeginPos = {x = x, y = y}
-- CCTOUCHBEGAN event must return true
return true
end
local function onTouchMoved(x, y)
local nMoveY = y - BeginPos.y
local curPosx, curPosy = MainMenu:getPosition()
local nextPosy = curPosy + nMoveY
local winSize = CCDirector:sharedDirector():getWinSize()
if nextPosy < 0 then
MainMenu:setPosition(0, 0)
return
end
if nextPosy > ((Test_Table.TESTS_COUNT + 1) * LINE_SPACE - winSize.height) then
MainMenu:setPosition(0, ((Test_Table.TESTS_COUNT + 1) * LINE_SPACE - winSize.height))
return
end
MainMenu:setPosition(curPosx, nextPosy)
BeginPos = {x = x, y = y}
CurPos = {x = curPosx, y = nextPosy}
end
local function onTouch(eventType, x, y)
if eventType == "began" then
return onTouchBegan(x, y)
elseif eventType == "moved" then
return onTouchMoved(x, y)
end
end
menuLayer:setTouchEnabled(true)
menuLayer:registerScriptTouchHandler(onTouch)
return menuLayer
end
其实就是在界面上创建很多menu.
首先
-- add menu items for tests
local MainMenu = CCMenu:create()
for index, labelName in pairs(Test_Name) do
local testLabel = CCLabelTTF:create(labelName, "Arial", 20)
local testMenuItem = CCMenuItemLabel:create(testLabel)
testMenuItem:registerScriptTapHandler(menuCallback)
testMenuItem:setPosition(ccp(s.width / 2, (s.height - (index + 1) * LINE_SPACE)))
MainMenu:addChild(testMenuItem, index + 10000, index + 10000)
end
然后menucallback里就会相应点击事件,创建不同的sence:
local function menuCallback(tag)
print(tag)
local Idx = tag - 10000
local testScene = CreateTestScene(Idx)
if testScene then
CCDirector:sharedDirector():replaceScene(testScene)
end
end
比如点击了ActionsTest;
执行的代码就是:
menucallback被调用,之后这段代码被执行:
-- create scene
local function CreateTestScene(nIdx)
CCDirector:sharedDirector():purgeCachedData()
local scene = nil
if nIdx == Test_Table.TEST_A
scene = ActionsTest()
这个Test_Table是tests.lua建立好的一个table;
Test_Table.TSET_A指向table的第一个元素(id==1)(name=="TEST_ACTIONS")。然后执行ActionTest()函数,看下函数的实现:
function ActionsTest()
cclog("ActionsTest")
local scene = CCScene:create()
Helper.createFunctionTable = {
ActionManual,
ActionMove,
ActionScale,
ActionRotate,
ActionSkew,
ActionRotationalSkewVSStandardSkew,
ActionSkewRotate,
ActionJump,
ActionCardinalSpline,
ActionCatmullRom,
ActionBezier,
ActionBlink,
ActionFade,
ActionTint,
ActionAnimate,
ActionSequence,
ActionSequence2,
ActionSpawn,
ActionReverse,
ActionDelaytime,
ActionRepeat,
ActionRepeatForever,
ActionRotateToRepeat,
ActionRotateJerk,
ActionCallFunc,
ActionCallFuncND,
ActionReverseSequence,
ActionReverseSequence2,
ActionOrbit,
ActionFollow,
ActionTargeted,
PauseResumeActions,
ActionIssue1305,
ActionIssue1305_2,
ActionIssue1288,
ActionIssue1288_2,
ActionIssue1327
}
scene:addChild(ActionManual())
scene:addChild(CreateBackMenuItem())
return scene
end
这个helper是个数组,这就是lua的强大了,可以把什么东西(不管类型)都塞进一个数组。
它的action是一个数组,在界面可以点击切换,看不同action的效果:
比如中间的那个大叔精灵的控制就在这:
local function ActionIssue1327()
local layer = CCLayer:create()
initWithLayer(layer)
centerSprites(0)
local spr = CCSprite:create("Images/grossini.png")
spr:setPosition(ccp(100, 100))
layer:addChild(spr)
local act1 = CCCallFuncN:create(logSprRotation)
local act2 = CCRotateBy:create(0.25, 45)
local act3 = CCCallFuncN:create(logSprRotation)
local act4 = CCRotateBy:create(0.25, 45)
local act5 = CCCallFuncN:create(logSprRotation)
local act6 = CCRotateBy:create(0.25, 45)
local act7 = CCCallFuncN:create(logSprRotation)
local act8 = CCRotateBy:create(0.25, 45)
local act9 = CCCallFuncN:create(logSprRotation)
local array = CCArray:create()
array:addObject(act1)
array:addObject(act2)
array:addObject(act3)
array:addObject(act4)
array:addObject(act5)
array:addObject(act6)
array:addObject(act7)
array:addObject(act8)
array:addObject(act9)
spr:runAction(CCSequence:create(array))
Helper.titleLabel:setString("Issue 1327")
Helper.subtitleLabel:setString("See console: You should see: 0, 45, 90, 135, 180")
return layer
end
好了。暂时分析到这里把。