我们的Lua项目的入口在哪里呢?
打开win32项目,找到AppDelegate.cpp,打开查看:
#include “cocos2d.h”
#include “AppDelegate.h”
#include “CCLuaEngine.h”
#include “audio/include/SimpleAudioEngine.h”
#include “lua_assetsmanager_test_sample.h”
using namespace CocosDenshion;
USING_NS_CC;
AppDelegate::AppDelegate()
{
}
AppDelegate::~AppDelegate()
{
SimpleAudioEngine::end();
}
bool AppDelegate::applicationDidFinishLaunching()
{
// 获得导演类实例
auto director = Director::getInstance();
// 获取渲染所有东西的 EGLView NA NA
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLView::createWithRect(“Lua Tests”, Rect(0,0,900,640));
director->setOpenGLView(glview);
}
// turn on display FPS
// 打开显示帧屏
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don’t call this
// 设置游戏画面每秒显示的帧数,默认是60帧。
director->setAnimationInterval(1.0 / 60);
// 获得屏幕大小
auto screenSize = glview->getFrameSize();
// 获得设计大小
auto designSize = Size(480, 320);
if (screenSize.height > 320)
{
auto resourceSize = Size(960, 640);
director->setContentScaleFactor(resourceSize.height/designSize.height);
}
// 设置屏幕设计分辨率
glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::FIXED_HEIGHT);
// register lua engine
LuaEngine* pEngine = LuaEngine::getInstance();
ScriptEngineManager::getInstance()->setScriptEngine(pEngine);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
LuaStack* stack = pEngine->getLuaStack();
register_assetsmanager_test_sample(stack->getLuaState());
#endif
// 执行脚本语言
pEngine->executeScriptFile(“src/controller.lua”);
return true;
}
// This function will be called when the app is inactive. When comes a phone call,it’s be invoked too
void AppDelegate::applicationDidEnterBackground()
{
Director::getInstance()->stopAnimation();
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
Director::getInstance()->startAnimation();
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
我们会发现代码中这么一段:
pEngine->executeScriptFile(“src/controller.lua”);
这个就是我们的入口文件,我们执行Lua的代码是从controller.lua这个文件开始的。
我们用LDT查看一下它的代码:
– avoid memory leak
– 避免内存泄漏
collectgarbage(“setpause”, 100)
collectgarbage(“setstepmul”, 5000)
require “src/mainMenu”
– run
local glView = cc.Director:getInstance():getOpenGLView()
local screenSize = glView:getFrameSize()
local designSize = {width = 480, height = 320}
local fileUtils = cc.FileUtils:getInstance()
if screenSize.height > 320 then
local searchPaths = {}
table.insert(searchPaths, “hd”)
fileUtils:setSearchPaths(searchPaths)
end
local targetPlatform = cc.Application:getInstance():getTargetPlatform()
local resPrefix = “”
if cc.PLATFORM_OS_IPAD == targetPlatform or cc.PLATFORM_OS_IPHONE == targetPlatform or cc.PLATFORM_OS_MAC == targetPlatform then
resPrefix = “”
else
resPrefix = “res/”
end
local searchPaths = fileUtils:getSearchPaths()
table.insert(searchPaths, 1, resPrefix)
table.insert(searchPaths, 1, resPrefix … “cocosbuilderRes”)
if screenSize.height > 320 then
table.insert(searchPaths, 1, resPrefix … “hd”)
table.insert(searchPaths, 1, resPrefix … “ccs-res”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/Images”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/ArmatureComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/AttributeComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/BackgroundComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/EffectComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/LoadSceneEdtiorFileTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/ParticleComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/SpriteComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/TmxMapComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/UIComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/hd/scenetest/TriggerTest”)
else
table.insert(searchPaths, 1, resPrefix … “ccs-res/Images”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/ArmatureComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/AttributeComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/BackgroundComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/EffectComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/LoadSceneEdtiorFileTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/ParticleComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/SpriteComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/TmxMapComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/UIComponentTest”)
table.insert(searchPaths, 1, resPrefix … “ccs-res/scenetest/TriggerTest”)
end
fileUtils:setSearchPaths(searchPaths)
– 创建场景
local scene = cc.Scene:create()
– 添加菜单
scene:addChild(CreateTestMenu())
if cc.Director:getInstance():getRunningScene() then
cc.Director:getInstance():replaceScene(scene)
else
– 根据给定的场景进入 Director的主循环 只能调用他运行你的第一个场景.
cc.Director:getInstance():runWithScene(scene)
end
然后我们就可以根据这个,来理清整个测试项目的脉路,我们也许想知道,例子中的那些菜单是怎么显示出来的,例子中又是如何进行切换场景的。只要我们知道如何开始了,以后我们就可以慢慢分析每一个例子所给我提供的代码。
在controller.lua文件中引入了maniMenu.lua
通过这个语句:require "src/mainMenu"
我们来看看mainMenu的代码:
– 引入资源文件
require “Cocos2d”
require “Cocos2dConstants”
require “Opengl”
require “OpenglConstants”
require “StudioConstants”
require “GuiConstants”
require “src/helper”
require “src/testResource”
require “src/VisibleRect”
require “src/AccelerometerTest/AccelerometerTest”
require “src/ActionManagerTest/ActionManagerTest”
require “src/ActionsEaseTest/ActionsEaseTest”
require “src/ActionsProgressTest/ActionsProgressTest”
require “src/ActionsTest/ActionsTest”
require “src/AssetsManagerTest/AssetsManagerTest”
require “src/BugsTest/BugsTest”
require “src/ClickAndMoveTest/ClickAndMoveTest”
require “src/CocosDenshionTest/CocosDenshionTest”
require “src/CocoStudioTest/CocoStudioTest”
require “src/CurrentLanguageTest/CurrentLanguageTest”
require “src/DrawPrimitivesTest/DrawPrimitivesTest”
require “src/EffectsTest/EffectsTest”
require “src/EffectsAdvancedTest/EffectsAdvancedTest”
require “src/ExtensionTest/ExtensionTest”
require “src/FontTest/FontTest”
require “src/IntervalTest/IntervalTest”
require “src/KeypadTest/KeypadTest”
require “src/LabelTest/LabelTest”
require “src/LabelTestNew/LabelTestNew”
require “src/LayerTest/LayerTest”
require “src/MenuTest/MenuTest”
require “src/MotionStreakTest/MotionStreakTest”
require “src/NewEventDispatcherTest/NewEventDispatcherTest”
require “src/NodeTest/NodeTest”
require “src/OpenGLTest/OpenGLTest”
require “src/ParallaxTest/ParallaxTest”
require “src/ParticleTest/ParticleTest”
require “src/PerformanceTest/PerformanceTest”
require “src/RenderTextureTest/RenderTextureTest”
require “src/RotateWorldTest/RotateWorldTest”
require “src/Sprite3DTest/Sprite3DTest”
require “src/SpriteTest/SpriteTest”
require “src/SceneTest/SceneTest”
require “src/SpineTest/SpineTest”
require “src/Texture2dTest/Texture2dTest”
require “src/TileMapTest/TileMapTest”
require “src/TouchesTest/TouchesTest”
require “src/TransitionsTest/TransitionsTest”
require “src/UserDefaultTest/UserDefaultTest”
require “src/ZwoptexTest/ZwoptexTest”
require “src/LuaBridgeTest/LuaBridgeTest”
require “src/XMLHttpRequestTest/XMLHttpRequestTest”
require “src/PhysicsTest/PhysicsTest”
– 行间距
local LINE_SPACE = 40
– 当前位置
local CurPos = {x = 0, y = 0}
– 开始位置
loca
l BeginPos = {x = 0, y = 0}
– 定义一张表
local _allTests = {
{ isSupported = true, name = “Accelerometer” , create_func= AccelerometerMain },
{ isSupported = true, name = “ActionManagerTest” , create_func = ActionManagerTestMain },
{ isSupported = true, name = “ActionsEaseTest” , create_func = EaseActionsTest },
{ isSupported = true, name = “ActionsProgressTest” , create_func = ProgressActionsTest },
{ isSupported = true, name = “ActionsTest” , create_func = ActionsTest },
{ isSupported = true, name = “AssetsManagerTest” , create_func = AssetsManagerTestMain },
{ isSupported = false, name = “Box2dTest” , create_func= Box2dTestMain },
{ isSupported = false, name = “Box2dTestBed” , create_func= Box2dTestBedMain },
{ isSupported = true, name = “BugsTest” , create_func= BugsTestMain },
{ isSupported = false, name = “ChipmunkAccelTouchTest” , create_func= ChipmunkAccelTouchTestMain },
{ isSupported = true, name = “ClickAndMoveTest” , create_func = ClickAndMoveTest },
{ isSupported = true, name = “CocosDenshionTest” , create_func = CocosDenshionTestMain },
{ isSupported = true, name = “CocoStudioTest” , create_func = CocoStudioTestMain },
{ isSupported = false, name = “CurlTest” , create_func= CurlTestMain },
{ isSupported = true, name = “CurrentLanguageTest” , create_func= CurrentLanguageTestMain },
{ isSupported = true, name = “DrawPrimitivesTest” , create_func= DrawPrimitivesTest },
{ isSupported = true, name = “EffectsTest” , create_func = EffectsTest },
{ isSupported = true, name = “EffectAdvancedTest” , create_func = EffectAdvancedTestMain },
{ isSupported = true, name = “ExtensionsTest” , create_func= ExtensionsTestMain },
{ isSupported = true, name = “FontTest” , create_func = FontTestMain },
{ isSupported = true, name = “IntervalTest” , create_func = IntervalTestMain },
{ isSupported = true, name = “KeypadTest” , create_func= KeypadTestMain },
{ isSupported = true, name = “LabelTest” , create_func = LabelTest },
{ isSupported = true, name = “LabelTestNew” , create_func = LabelTestNew },
{ isSupported = true, name = “LayerTest” , create_func = LayerTestMain },
{ isSupported = true, name = “LuaBridgeTest” , create_func = LuaBridgeMainTest },
{ isSupported = true, name = “MenuTest” , create_func = MenuTestMain },
{ isSupported = true, name = “MotionStreakTest” , create_func = MotionStreakTest },
{ isSupported = false, name = “MutiTouchTest” , create_func= MutiTouchTestMain },
{ isSupported = true, name = “NewEventDispatcherTest” , create_func = NewEventDispatcherTest },
{ isSupported = true, name = “NodeTest” , create_func = CocosNodeTest },
{ isSupported = true, name = “OpenGLTest” , create_func= OpenGLTestMain },
{ isSupported = true, name = “ParallaxTest” , create_func = ParallaxTestMain },
{ isSupported = true, name = “ParticleTest” , create_func = ParticleTest },
{ isSupported = true, name = “PerformanceTest” , create_func= PerformanceTestMain },
{ isSupported = true, name = “PhysicsTest” , create_func = PhysicsTest },
{ isSupported = true, name = “RenderTextureTest” , create_func = RenderTextureTestMain },
{ isSupported = true, name = “RotateWorldTest” , create_func = RotateWorldTest },
{ isSupported = true, name = “SceneTest” , create_func = SceneTestMain },
{ isSupported = true, name = “SpineTest” , create_func = SpineTestMain },
{ isSupported = false, name = “SchdulerTest” , create_func= SchdulerTestMain },
{ isSupported = false, name = “ShaderTest” , create_func= ShaderTestMain },
{ isSupported = true, name = “Sprite3DTest” , create_func = Sprite3DTest },
{ isSupported = true, name = “SpriteTest” , create_func = SpriteTest },
{ isSupported = false, name = “TextInputTest” , create_func= TextInputTestMain },
{ isSupported = true, name = “Texture2DTest” , create_func = Texture2dTestMain },
{ isSupported = false, name = “TextureCacheTest” , create_func= TextureCacheTestMain },
{ isSupported = true, name = “TileMapTest” , create_func = TileMapTestMain },
{ isSupported = true, name = “TouchesTest” , create_func = TouchesTest },
{ isSupported = true, name = “TransitionsTest” , create_func = TransitionsTest },
{ isSupported = true, name = “UserDefaultTest” , create_func= UserDefaultTestMain },
{ isSupported = true, name = “XMLHttpRequestTest” , create_func = XMLHttpRequestTestMain },
{ isSupported = true, name = “ZwoptexTest” , create_func = ZwoptexTestMain }
}
local TESTS_COUNT = table.getn(_allTests)
– create scene 创建场景
local function CreateTestScene(nIdx)
cc.Director:getInstance():purgeCachedData()
local scene = _allTests[nIdx].create_func()
return scene
end
– create menu 创建菜单
function CreateTestMenu()
– 菜单层
local menuLayer = cc.Layer:create()
local function closeCallback()
– 结束执行,释放正在运行的场景。
cc.Director:getInstance():endToLua()
end
– 菜单回调
local function menuCallback(tag)
print(tag)
local Idx = tag - 10000
local testScene = CreateTestScene(Idx)
true, name = “ZwoptexTest” , create_func = ZwoptexTestMain }
}
local TESTS_COUNT = table.getn(_allTests)
– create scene 创建场景
local function CreateTestScene(nIdx)
cc.Director:getInstance():purgeCachedData()
local scene = _allTests[nIdx].create_func()
return scene
end
– create menu 创建菜单
function CreateTestMenu()
– 菜单层
local menuLayer = cc.Layer:create()
local function closeCallback()
– 结束执行,释放正在运行的场景。
cc.Director:getInstance():endToLua()
end
– 菜单回调
local function menuCallback(tag)
print(tag)
local Idx = tag - 10000
local testScene = CreateTestScene(Idx)