前言

  本文实现了spine动画的预加载,解决在战斗等大量加载spine动画的时候出现卡顿现象。

这里使用和修改三个类,直接修改的源码,当然你也可以继承LuaSkeletonAnimation,自己封装一个类。这里做个例子,

不自己写类,直接改源码。如果想自己写,绑定到lua,看我别的帖子。废话不多说,入主题。

在之前对图片 应该对图片进行异步加载,详细以后再说。

版本:quicklua 3.3  win vs2012

原理

先分析下,LuaSkeletonAnimation类

class LuaSkeletonAnimation: public spine::SkeletonAnimation {
public:
static LuaSkeletonAnimation* createWithFile (const char* skeletonDataFile, const char* atlasFile, float scale = ); LuaSkeletonAnimation (const char* skeletonDataFile, const char* atlasFile, float scale = ); virtual ~LuaSkeletonAnimation();
};

LuaSkeletonAnimation 继承自 SkeletonAnimation类。

在lua_cocos2dx_spine_manual.cpp中可以看到,在lua层调用 create 方法实际是 调用的LuaSkeletonAnimation的 createWithFile 方法

所以我们把新的方法也放到这里,当然你也可以直接放到SkeletonAnimation类中。建议在此处。

再看下SkeletonAnimation类:

    static SkeletonAnimation* createWithData (spSkeletonData* skeletonData, bool ownsSkeletonData = false);
static SkeletonAnimation* createWithFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale = );
static SkeletonAnimation* createWithFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = );

创建spine动画有两种方法,分别是createwithfile和createwithdata。

createWithFile是通过加载动作数据马上进行创建,如果spine动画中的json文件大小超过100k时,会出现卡顿现象,如果动画文件偏小,可以使用这个方法来创建动画。createWithData是通过预加载,保存动画数据在spSkeletonData中,然后通过实现创建动画。

上面这些我相信你们一看就懂吧。 总体来讲:

lua 层使用,createwithfile 方法创建,然后保存在table 中,实现预加载 。在使用的时候使用createWithData,创建动画。

createWithData方法需要的参数是spSkeletonData,我们需要封装一个获取spSkeletonData的方法,具体看LuaSkeletonAnimation的爷爷类。

c++代码的修改

(1)SkeletonAnimation.h:

spSkeletonData* getSkeletonData();

  SkeletonAnimation.cpp 实现

spSkeletonData* SkeletonAnimation::getSkeletonData(){

   spSkeletonData*skData =SkeletonRenderer::getSkeleton()->data;

   return skData;
}

(2)LuaSkeletonAnimation.h

class LuaSkeletonAnimation: public spine::SkeletonAnimation {
public:
static LuaSkeletonAnimation* createWithFile (const char* skeletonDataFile, const char* atlasFile, float scale = ); // preload spine 添加下面两个方法
static LuaSkeletonAnimation* createWithSkeletonAnimation(SkeletonAnimation* skeletonAnimation);
LuaSkeletonAnimation(SkeletonAnimation* spineData); LuaSkeletonAnimation (const char* skeletonDataFile, const char* atlasFile, float scale = ); virtual ~LuaSkeletonAnimation();
};

说明:

createWithSkeletonAnimation方法,为了减少导出自定义类,所以参数是 SkeletonAnimation。
LuaSkeletonAnimation::LuaSkeletonAnimation (const char* skeletonDataFile, const char* atlasFile, float scale)
: spine::SkeletonAnimation(skeletonDataFile, atlasFile, scale)
{ } LuaSkeletonAnimation::LuaSkeletonAnimation(SkeletonAnimation* spineData)
: spine::SkeletonAnimation(spineData->getSkeletonData())
{ }
LuaSkeletonAnimation::~LuaSkeletonAnimation()
{
ScriptHandlerMgr::getInstance()->removeObjectAllHandlers((void*)this);
} LuaSkeletonAnimation* LuaSkeletonAnimation::createWithFile (const char* skeletonDataFile, const char* atlasFile, float scale)
{
LuaSkeletonAnimation* node = new (std::nothrow) LuaSkeletonAnimation(skeletonDataFile, atlasFile, scale);
node->autorelease();
return node;
} LuaSkeletonAnimation* LuaSkeletonAnimation::createWithSkeletonAnimation(SkeletonAnimation* skeletonAnimation)
{
LuaSkeletonAnimation* node = new (std::nothrow) LuaSkeletonAnimation(skeletonAnimation);
node->autorelease();
return node;
}

改好上面两个类。可以使用cocos2dx自带的脚本重新tolua一下。这里 直接改代码吧,省时有效。。。嘎嘎、、

lua_cocos2dx_spine_manual.cpp

1.

static int lua_cocos2dx_CCSkeletonAnimation_createWithSkeletonAnimation(lua_State* L)
{
if (nullptr == L)
return ; int argc = ; #if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertable(L,,"sp.SkeletonAnimation",,&tolua_err)) goto tolua_lerror;
#endif argc = lua_gettop(L) - ; if ( == argc)
{
//TODO
SkeletonAnimation* skeletonAnimation = (SkeletonAnimation*)tolua_tousertype(L, , );
auto tolua_ret = LuaSkeletonAnimation::createWithSkeletonAnimation(skeletonAnimation); int nID = (tolua_ret) ? (int)tolua_ret->_ID : -;
int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL;
toluafix_pushusertype_ccobject(L, nID, pLuaID, (void*)tolua_ret,"sp.SkeletonAnimation");
return ;
}
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'createWithSkeletonAnimation'.",&tolua_err);
#endif
return ;
}

2.static void extendCCSkeletonAnimation(lua_State* L)方法中添加一行:

 tolua_function(L, "createWithSkeletonAnimation", lua_cocos2dx_CCSkeletonAnimation_createWithSkeletonAnimation);

lua使用及预加载

上面代码改好之后,编译一下,跑起来程序,在lua测试一下,能不能正常调用函数。OK的话,就可以写代码  预加载了。

在项目中我们一般要有个loadlayer 吧需要的音乐 图片 spine等预加载进去。这里测试一下就不写了、

首先我们要封装一下 sp.SkeletonAnimation

local SpineNode = class("SpineNode", function(spineName)
spineName = "ceshi"
--预加载列表有,就直接使用
if LoadingManager:isSpineLoad(spineName) then
dump("****************preload:"..spineName)
do return sp.SkeletonAnimation:createWithSkeletonAnimation(LoadingManager.preloadSpines[spineName]) end
end if not cc.FileUtils:getInstance():isFileExist(spineName..".json") then
print("Ani ERROR: not found " .. spineName .. "\n")
print(debug.traceback("", ))
spineName = "ceshi"
end local s_pPathSpineBoyJson = spineName..".json"
local s_pPathSpineBoyAtlas = spineName..".atlas" return sp.SkeletonAnimation:create(s_pPathSpineBoyJson, s_pPathSpineBoyAtlas, )
end)

其他需要 的方法自己封装几个。这里不写了。

在整个统一的预加载资源类 ,提前加载spine 等,就可以了。

注释:

可以使用 createWithData ,添加一个 getAnimationData的方法。原理是一样的。

以后会追加一篇关于lua异步加载 spine图片的文章。

追加:

先create 一个母本,在添加到缓存数组的时候,记得retain一下。最后记得释放release。

要不然创建母本之后,会自己释放掉,导致直接崩溃。

总结

  兄弟们那,一定要多看源码啊。。。流程应该很清晰了吧、。、、少年们应该能看懂了。交流加我QQ776274781 。大神勿喷。。

spine实现预加载(一)的更多相关文章

  1. 使用lua实现Spine动画的预加载

    创建spine动画有两种方法,分别是createwithfile和createwithdata. createWithFile是通过加载动作数据马上进行创建,如果spine动画中的json文件大小超过 ...

  2. Javascript图片预加载详解

    预加载图片是提高用户体验的一个很好方法.图片预先加载到浏览器中,访问者便可顺利地在你的网站上冲浪,并享受到极快的加载速度.这对图片画廊及图片占据很大比例的网站来说十分有利,它保证了图片快速.无缝地发布 ...

  3. IIS初始化(预加载),解决第一次访问慢,程序池被回收问题

    你以为你可以慢,那是不可能的!你以为你可以不动,那也是不可能的! 河南是守株待兔故事情节的发源地,讲的是懒惰的农夫坐在树桩旁等待可爱的小毛兔撞树的故事,那么这种事情怎么可能天天出现呢!你以为的事并一定 ...

  4. FragmentPagerAdapter加载fragment并使用setUserVisibleHint()处理预加载时遇到的坑,给textview赋值时出现的空指针异常

    FragmentPagerAdapter加载fragment并使用setUserVisibleHint()处理预加载时,给textview赋值时出现的空指针异常 public class BaseFr ...

  5. ViewPager+Fragment取消预加载(延迟加载)(转)

    原文:http://www.2cto.com/kf/201501/368954.html 在项目中,都或多或少地使用的Tab布局,所以大都会用到ViewPager+Fragment,但是Fragmen ...

  6. js图片预加载

    图片预加载有大体有几种方式 1.html标签或css加载图片. 显而易见我们使用img标签或者通过标签的background-image属性都可以实现图片的预加载.但是为了避免初次载入过多图片影响体验 ...

  7. 利用简洁的图片预加载组件提升h5移动页面的用户体验

    在做h5移动页面,相信大家一定碰到过页面已经打开,但是里面的图片还未加载出来的情况,这种问题虽然不影响页面的功能,但是不利于用户体验.抛开网速的原因,解决这个问题有多方面的思路:最基本的,要从http ...

  8. ASP.NET MVC3 Razor 调试与预加载

    目录(?)[-] 获取服务器信息 FormsAuthenticationSlidingExpiration 属性 MVC3预加载   在ASP.NET MVC3开发中,调试中怎么也是不可缺少的,那对于 ...

  9. Javascript实现图片预加载【回调函数,多张图片】

    使用JS实现一组图片动画效果或者使用HTML5 Canvas渲染一系列图片等案例中,需要图片全部加载完成方可运行动画效果.此时程序中就会涉及多张图片预加载代码.当接二连三的案例中都涉及图片预加载时,就 ...

随机推荐

  1. parallelism

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Traditionally, the co ...

  2. mysql slow query---pt-query-digest----db structure consistency,monitor table records before and after transaction.

    将数据库脚本纳入版本管理是很必要的,尤其对于需要在客户那里部署升级的系统. 对于Python Django等框架,由于数据库是通过Model生成的,因此框架本身包括数据库升级工具,并通过代码版本间接管 ...

  3. BasicDataSource的配置参数

    参数 描述 username 传递给JDBC驱动的用于建立连接的用户名 password 传递给JDBC驱动的用于建立连接的密码 url 传递给JDBC驱动的用于建立连接的URL driverClas ...

  4. 由于某IP大频率提交评论导致服务器宕机

    早上突然收到dnspod的宕机通知(好久没收到了,有点手足无措). 服务器在上午10:40时达到85%.uptime显示cpu利用率达到35.不宕才怪. 按照之前的经验,应该是触发一个特别耗CPU的处 ...

  5. Xcode使用HTTP配置

    Xcode7 出现获取网络请求时出现如下异常: App Transport Security has blocked a cleartext HTTP (http://) resource load ...

  6. spring-amqp 动态创建queue、exchange、binding

    pom.xml <!-- mq 依赖 --> <dependency> <groupId>com.rabbitmq</groupId> <arti ...

  7. web app上传图片

    很就很久以前,web app上传图片需要通过cordova插件,那时候好像还叫phonegap. 后来一个html标签就可以了 <input type="file" clas ...

  8. 关于UIView的方法animateWithDuration:animations:completion:的说明

    今天遇到一个问题,具体问题就不细说了,总之是UIView的动画导致的. 研究结果表明,UIViewController被挡住或没显示出来时,用UIView的静态方法animateWithDuratio ...

  9. EF 保证线程内唯一 上下文的创建

    1.ef添加完这个对象,就会自动返回这个对象数据库的内容,比如下面这个表是自增ID 最后打印出来的ID  就是自增的结果 2.lambda 中怎么select * var userInfoList = ...

  10. iOS,应用崩溃日志分析

    参考资料:http://www.cocoachina.com/industry/20130725/6677.html 1.获得崩溃日志 2.崩溃日志实例 3.符号化崩溃日志 4.低内存闪退 获得崩溃日 ...