`
473687880
  • 浏览: 535630 次
文章分类
社区版块
存档分类
最新评论

cocos2d-x之testlua学习

 
阅读更多

从下面代码可以看出

    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

好了。暂时分析到这里把。


分享到:
评论

相关推荐

    Cocos2d-x实战:JS卷——Cocos2d-JS开发

    资源名称:Cocos2d-x实战:JS卷——Cocos2d-JS开发内容简介:本书是介绍Cocos2d-x游戏编程和开发技术书籍,介绍了使用Cocos2d-JS中核心类、瓦片地图、物理引擎、音乐音效、数据持久化、网络通信、性能优化、多平台...

    Cocos2d-x 3.x游戏开发实战pdf含目录

    《Cocos2d-x 3.x游戏开发实战》是一本深度探讨Cocos2d-x 3.x框架的游戏开发书籍,适合对游戏编程有兴趣的开发者学习。Cocos2d-x 是一个开源的、跨平台的2D游戏开发框架,广泛应用于iOS、Android、Windows等多平台的...

    Cocos2d-x实战:C++卷(2版)源代码

    通过学习这些源代码,开发者不仅能理解Cocos2d-x的基本用法,还能学习到游戏开发的最佳实践和高级技巧。对于初学者,这是一个绝佳的动手实践机会;对于经验丰富的开发者,它提供了深入框架内部的窗口。总之,《Cocos...

    cocos2d-x-3.1.zip

    总的来说,Cocos2d-x 3.1是游戏开发者学习2D游戏开发的一个重要参考点,虽然它已经不是最新的版本,但其核心概念和机制在后续版本中仍然保持一致,是理解整个Cocos2d-x框架的基础。解压并研究“cocos2d-x-3.1”中的...

    cocos2d-x_v3.16安装及环境变量配置文档

    cocos2d-x 是一个开源的游戏开发框架,使用 C++ 语言编写,支持多平台发布,包括 iOS、Android、Windows、macOS、Linux 和 Web。cocos2d-x v3.16 是该框架的一个版本号,本文档主要介绍了该版本的安装流程以及环境...

    cocos2d-x windows vs2010配置

    "cocos2d-x windows vs2010 配置详解" 本文将详细介绍如何在 Windows 环境下使用 Visual Studio 2010 配置 Cocos2d-x 游戏引擎。Cocos2d-x 是一个跨平台的游戏引擎,可以在多种平台上运行,包括 Windows、Mac OS X...

    cocos2d-x-3.13.1 spine3.6.zip

    《Cocos2d-x 3.13.1与Spine 3.6集成详解》 Cocos2d-x是一个广泛使用的开源游戏开发框架,它基于C++,同时支持Lua和JavaScript等多种脚本语言,为开发者提供了高效、跨平台的游戏开发解决方案。在3.13.1版本中,...

    经典版本 方便下载 源码 旧版本 3.8 官网找不到了 cocos2d-x-3.8.zip

    这个“cocos2d-x-3.8.zip”压缩包包含的是cocos2d-x的经典版本3.8,对于想要学习旧版引擎或者需要回溯历史代码的开发者来说,这是一个非常宝贵资源。 cocos2d-x 3.8 版本的主要特性包括: 1. **跨平台支持**:支持...

    cocos2d-x API中文文档

    这份API文档不仅提供了关于Cocos2d-x的基本信息,还深入到具体的技术细节,对于学习和使用cocos2d-x开发游戏的开发者来说,是非常宝贵的参考资料。通过这份文档,开发者可以了解如何利用cocos2d-x的API构建游戏场景...

    cocos2d-x-cocos2d-x-2.2.2.zip

    总的来说,cocos2d-x 2.2.2是一个功能完备、易于学习和使用的2D游戏开发框架。通过深入理解并掌握这个版本,开发者可以创建出运行在多种平台上的高质量游戏,享受到cocos2d-x带来的强大开发体验。无论是初学者还是...

    cocos2d-x 动画工具 Flash2Cocos2d-x 1.3

    在压缩包文件"jyinkailej-Flash2Cocos2d-x-8c0deff"中,可能包含了Flash2Cocos2d-x工具的源码、文档、示例项目和其他相关资源,供开发者学习和使用。这些资源可以帮助开发者深入理解如何使用该工具,并进行实际的...

    Cocos2d-x实战C++卷关东升著完整版pdf

    总之,《Cocos2d-x实战C++卷》全面覆盖了Cocos2d-x游戏开发的各个环节,从基础到进阶,为读者提供了一条清晰的学习路径。通过阅读并实践书中的示例,读者可以逐步成长为熟练的Cocos2d-x游戏开发者。

    Cocos2d-x实战 JS卷 Cocos2d-JS开发

    《Cocos2d-x实战 JS卷 Cocos2d-JS开发》是一本深入探讨Cocos2d-...总之,这本书是JavaScript开发者进入Cocos2d-x世界的一把钥匙,通过深入学习,开发者可以利用Cocos2d-JS的强大功能,创造出富有创意和吸引力的2D游戏。

    Cocos2d-x学习笔记

    Cocos2d-x框架除了Windows平台外,还支持iOS、Android、Mac OS X、Web等平台,初学者在掌握了Windows平台的开发后,可以进一步学习如何使用Cocos2d-x跨平台开发,实现一个游戏在多个平台上运行。 在学习过程中,...

    cocos2d-x 3.0

    1. 学习cocos2d-x 3.0的官方文档,了解其核心类和API。 2. 研究cocos2d-x社区的教程和示例项目,积累实战经验。 3. 探索Box2D物理引擎,提升游戏的物理表现力。 4. 实践编程,通过修改和调试代码,理解行走逻辑的每...

    Cocos2d-x实战++JS卷++Cocos2d-JS开发+PDF电子书下载+带书签目录+完整

    标题中提到了"Cocos2d-x实战++JS卷++Cocos2d-JS开发+PDF电子书下载+带书签目录+完整",这里面包含了几个关键知识点: 1. Cocos2d-x:是一个开源的游戏开发框架,它主要用于开发跨平台的游戏和应用程序,支持iOS、...

    cocos引擎老版本集合(cocos2d-x-2.2.1 - 3.5).zip

    cocos引擎老版本下载集合(cocos2d-x-2.2.1 - 3.5),分别有cocos2d-x-3.5、cocos2d-x-3.4、cocos2d-x-3.2、cocos2d-x-2.2.6、cocos2d-x-2.2.2和cocos2d-x-2.2.1。

    cocos2d-x-2.1.4.rar

    5. 学习资源:cocos2d-x 社区活跃,有丰富的教程、文档和示例代码可供学习参考,帮助开发者快速上手。 总结,cocos2d-x 2.1.4版本是移动游戏开发者的强大工具,它的优化和新特性使得开发者能够更加高效地创建出高...

    使用cocos2d-x-2.0-2.0.4开发的简单跨平台益智类魔塔小游戏

    《使用cocos2d-x-2.0-2.0.4开发的简单跨平台益智类魔塔小游戏》...通过学习和分析这个项目,开发者不仅可以掌握cocos2d-x的基本用法,还能了解到益智类游戏的设计思路和实现技巧,为自己的游戏开发之路打下坚实的基础。

    Cocos2d-x实战_Lua卷 _第2版

    《Cocos2d-x实战_Lua卷_第2版》是一本专为游戏开发者准备的指南,主要聚焦于使用Cocos2d-x框架与Lua...通过学习这本书,你不仅可以学会如何利用Cocos2d-x和Lua构建游戏,还能掌握一套完整的游戏开发流程和最佳实践。

Global site tag (gtag.js) - Google Analytics