一年多了,感觉现在编辑器做的还是不错的了,今天就研究了下。
就拿cocos自带的MainScrean 来做试验了,选择cocos2dx.4.4final
创建好一个空lua项目后,第一步呢就是加载。
吐槽一下,我是知道新版花了加载用csloader,可是test里面的例子还是老的,既然换了方式为什么不把例子换掉呢。
我就研究了下我最关心的几个问题既 加载,屏幕适配,动画,回调。
一,加载ccos导出文件
把生成的资源在res下考到工程,用csloader加载
加载代码
local node = cc.CSLoader:createNode(“MainScreen.csb”)
self:addChild(node)
这样子整个场景就加载好了
二,屏幕适配问题
现在cocos支持相对布局了,所以适配就好做些了,只要编辑的适当,适配不是问题。
把config.lua 里改成这样子(针对竖屏游戏)
CC_DESIGN_RESOLUTION = {
width = 640,
height = 960,
autoscale = “FIXED_WIDTH”,
– callback = function(framesize)
– local ratio = framesize.width / framesize.height
– if ratio <= 1.34 then
– – iPad 7681024(15362048) is 4:3 screen
– return {autoscale = “FIXED_WIDTH”}
– end
– end
}
这样子无论屏幕怎么变宽的ui总能正确显示,高的部分可能显示不全或是有黑边,这时候就该相对布局出场了,
我们打开cocos双击资源里的MainScreen ,
在动画那里有个ProjectNode_8 ,双击选中它,
在右侧的属性面板 位置与尺寸 顶住上面的钉子,
说明该node相对顶端位置不变,这样子就保证他总是在屏幕最上面了。
发布资源在打开我发现位置还是没有变(差异),
于是有看源码研究下(没办法资源是在太少,官方也不给个完整示例又或许是我没找到吧),
在加载玩node后加入
local node = cc.CSLoader:createNode(“MainScreen.csb”)
self:addChild(node)
node:setContentSize(cc.size(display.width,display.height))
ccui.Helper:doLayout(node)
这样子就得到我们要的结果了。
三、动画既帧回调
这个比较好解决,看到了一篇帖子,可以自己去搜索,我就不说了直接上代码了(自己先随便做个帧动画,添加帧事件)
local node = cc.CSLoader:createNode(“MainScreen.csb”)
self:addChild(node)
node:setContentSize(cc.size(display.width,display.height))
ccui.Helper:doLayout(node)
local action = cc.CSLoader:createTimeline(“MainScreen.csb”)
node:runAction(action)
action:setFrameEventCallFunc(function (event)
dump(event:getEvent())
end)
action:gotoFrameAndPlay(0,130,false)
四、回调
帧事件的回调上面已经有了,下面我们就说其他控件的。
这个方面资料我没有找到,所以呢就直接自己改造了个,当然可能很菜,但是可以用。
改造开始----------------------------
首先呢 改CSLoader.h 加个addCallback方法
static cocostudio::timeline::ActionTimeline* createTimeline(const std::string& filename);
static void addCallback(cocostudio::WidgetCallBackHandlerProtocol _callbackHandler);
/
static cocostudio::timeline::ActionTimelineNode* createActionTimelineNode(const std::string& filename);
static cocostudio::timeline::ActionTimelineNode* createActionTimelineNode(const std::string& filename, int startIndex, int endIndex, bool loop);
*/
CSLoader.cpp实现
void CSLoader::addCallback(WidgetCallBackHandlerProtocol *_callbackHandler)
{
if(callbackHandler)
delete callbackHandler;
callbackHandler = _callbackHandler;
}
注掉bindCallback 方法里的第一行
// auto callbackHandler = dynamic_cast<WidgetCallBackHandlerProtocol *>(handler);
ok,修改完毕,我还是本着尽量少动cocos的东西,主要东西在lua绑定了
2 打开lua_cocos2dx_csloader_manual.cpp 懒得说了 直接考代码了
/****************************************************************************
Copyright © 2013-2014 Chukong Technologies Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
***************************************************************************/
#include “lua_cocos2dx_csloader_manual.hpp”
#include “tolua_fix.h”
#include “LuaBasicConversions.h”
#include “CCLuaValue.h”
#include “CCLuaEngine.h”
#include “ActionTimeline/CCActionTimeline.h”
#include “ActionTimeline/CSLoader.h”
#include “ActionTimeline/CCActionTimelineNode.h”
#include “cocostudio/WidgetCallBackHandlerProtocol.h”
int lua_cocos2dx_csloader_CSLoader_createTimeline(lua_State tolua_S)
{
int argc = 0;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertable(tolua_S,1,“cc.CSLoader”,0,&tolua_err)) goto tolua_lerror;
#endif
argc = lua_gettop(tolua_S) - 1;
if (argc == 1)
{
std::string arg0;
ok &= luaval_to_std_string(tolua_S, 2,&arg0, "cc.CSLoader:createTimeline");
if(!ok)
return 0;
cocostudio::timeline::ActionTimeline* ret = cocos2d::CSLoader::createTimeline(arg0);
object_to_luaval<cocostudio::timeline::ActionTimeline>(tolua_S, "ccs.ActionTimeline",(cocostudio::timeline::ActionTimeline*)ret);
return 1;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n ", "cc.CSLoader:createTimeline",argc, 1);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function ‘lua_cocos2dx_CSLoader_createTimeline’.",&tolua_err);
#endif
return 0;
}
std::unordered_map<std::string, int> name_functions;
class MyWidgetCallBackHandlerProtocol:public cocostudio::WidgetCallBackHandlerProtocol{
virtual cocos2d::ui::Widget::ccWidgetTouchCallback onLocateTouchCallback( std::string &callBackName)
{
int hander = 0;
if(name_functions.find(callBackName)!=name_functions.end() ){
hander = name_functions.at(callBackName);
}
else{
CCLOG("%s callback not fount",callBackName.c_str());
return nullptr;
}
return (Ref* ref,cocos2d::ui::Widget::TouchEventType type)->void{
LuaEngine::getInstance()->executeEvent(hander,callBackName.c_str(),ref);
};
//return nullptr;
};
virtual cocos2d::ui::Widget::ccWidgetClickCallback onLocateClickCallback(const std::string &callBackName)
{
int hander = 0;
if(name_functions.find(callBackName)!=name_functions.end() ){
hander = name_functions.at(callBackName);
}
else{
CCLOG("%s callback not fount",callBackName.c_str());
return nullptr;
}
return (Ref* ref)->void{
LuaEngine::getInstance()->executeEvent(hander,callBackName.c_str(),ref);
};
};
virtual cocos2d::ui::Widget::ccWidgetEventCallback onLocateEventCallback(const std::string &callBackName)
{
int hander = 0;
if(name_functions.find(callBackName)!=name_functions.end() ){
hander = name_functions.at(callBackName);
}
else{
CCLOG("%s callback not fount",callBackName.c_str());
return nullptr;
}
return (Ref* ref,int nouse)->void{
LuaEngine::getInstance()->executeEvent(hander,callBackName.c_str(),ref);
};
};
};
int lua_cocos2dx_csloader_CSLoader_cleanCallback(lua_State* tolua_S)
{
name_functions.clear();
cocos2d::CSLoader::addCallback(new MyWidgetCallBackHandlerProtocol());
}
int lua_cocos2dx_csloader_CSLoader_addCallback(lua_State* tolua_S)
{
if (NULL == tolua_S)
return 0;
int argc = 0;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
argc = lua_gettop(tolua_S) -1;
if (argc == 2)
{
std::string name;
ok &= luaval_to_std_string(tolua_S, 2,&name, "cc.CSLoader:addCallback");
if(!ok)
return 0;
#if COCOS2D_DEBUG >= 1
if(!toluafix_isfunction(tolua_S,3,“LUA_FUNCTION”,0,&tolua_err))
goto tolua_lerror;
#endif
LUA_FUNCTION handler = toluafix_ref_function(tolua_S,3,0);
name_functions.insert(std::pair<std::string, int>(name,handler));
return 0;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n", "cc.CSLoader:addCallback",argc, 2);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function ‘lua_cocos2dx_csloader_CSLoader_addCallback’.",&tolua_err);
return 0;
#endif
}
int register_all_cocos2dx_csloader_manual(lua_State* L)
{
lua_pushstring(L, “cc.CSLoader”);
lua_rawget(L, LUA_REGISTRYINDEX);
if (lua_istable(L,-1))
{
tolua_function(L, “createTimeline”, lua_cocos2dx_csloader_CSLoader_createTimeline);
tolua_function(L, "cleanCallback", lua_cocos2dx_csloader_CSLoader_cleanCallback);
tolua_function(L, "addCallback", lua_cocos2dx_csloader_CSLoader_addCallback);
}
lua_pop(L, 1);
return 0;
}
直接换了就好。
下面来说下使用。
cc.CSLoader:cleanCallback()
cc.CSLoader:addCallback(“clickHome”,function (name,ref)
print(name,ref)
end)
local node = cc.CSLoader:createNode(“MainScreen.csb”)
self:addChild(node)
node:setContentSize(cc.size(display.width,display.height))
ccui.Helper:doLayout(node)
local action = cc.CSLoader:createTimeline(“MainScreen.csb”)
node:runAction(action)
action:setFrameEventCallFunc(function (event)
dump(event:getEvent())
end)
action:gotoFrameAndPlay(0,130,false)
说明回调一定要加载CSLoader:createNode之前,
cc.CSLoader:cleanCallback()
cc.CSLoader:addCallback(“clickHome”,function (name,ref)
print(name,ref)
end)
不要反了,这样子回调基本ok了 。
手贱多鲁了点 3.4 就三点 我对多的那点没兴趣