标签:
创建spine动画有两种方法,分别是createwithfile和createwithdata。
createWithFile是通过加载动作数据马上进行创建,如果spine动画中的json文件大小超过100k时,会出现卡顿现象,如果动画文件偏小,可以使用这个方法来创建动画。
createWithData是通过预加载,保存动画数据在spSkeletonData中,然后通过实现创建动画,这个方法可以使用在spine动画偏大的情况下使用。
cocos2dx 自带的LuaSkeletonAnimation文件中没有对createWithData进行实现,所有,我选择继承SkeletonAnimation来重写该方法。
实现方法如下:
(SpineAnimation_new.h)
#include <stdio.h>
#include "cocos/editor-support/spine/SkeletonAnimation.h"
#include "cocos/editor-support/spine/spine.h"
#include "spine/SkeletonRenderer.h"
#include "cocos2d.h"
using namespace spine;
class SpineAnimation_new:spine::SkeletonAnimation
{
public:
static SpineAnimation_new *createWithSkeletonData(spSkeletonData* skeletonData);
static SpineAnimation_new *createWithSkeletonAnimation(SpineAnimation_new*skeletonAnim);
static SpineAnimation_new *createWithFileName(const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
virtual ~SpineAnimation_new();
spSkeletonData* getSkeletonData()
{
spSkeletonData*skData =SkeletonRenderer::getSkeleton()->data;
return skData;
}
private:
SpineAnimation_new(spSkeletonData*skeletonData);
SpineAnimation_new(const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
};
(SpineAnimation_new.cpp)
#include "SpineAnimation_new.h"
#include "cocos2d.h"
#include "cocos/scripting/lua-bindings/manual/cocos2d/LuaScriptHandlerMgr.h"
#include "CCLuaStack.h"
#include "CCLuaEngine.h"
USING_NS_CC;
SpineAnimation_new *SpineAnimation_new::createWithSkeletonData(spSkeletonData* skeletonData)
{
SpineAnimation_new* node = new (std::nothrow)SpineAnimation_new(skeletonData);
node->autorelease();
return node;
}
SpineAnimation_new *SpineAnimation_new::createWithSkeletonAnimation(SpineAnimation_new*skeletonAnim)
{
SpineAnimation_new* node = new (std::nothrow)SpineAnimation_new(skeletonAnim->getSkeletonData());
node->autorelease();
return node;
}
SpineAnimation_new *SpineAnimation_new::createWithFileName(const std::string& skeletonDataFile, const std::string& atlasFile, float scale)
{
SpineAnimation_new* node = new (std::nothrow)SpineAnimation_new(skeletonDataFile, atlasFile, scale);
node->autorelease();
return node;
}
SpineAnimation_new::~SpineAnimation_new()
{
ScriptHandlerMgr::getInstance()->removeObjectAllHandlers((void*)this);
}
SpineAnimation_new::SpineAnimation_new(spSkeletonData*skeletonData):
SkeletonAnimation(skeletonData)
{
}
SpineAnimation_new::SpineAnimation_new(const std::string& skeletonDataFile, const std::string& atlasFile, float scale):SkeletonAnimation(skeletonDataFile,atlasFile,scale)
{
}
在绑定lua的c++代码中,cocos2dx自带的打包工具会出现一下小问题,在栈中获取不了加载好的spine数据,所以要我将读栈方法修改如下:
argc = lua_gettop(tolua_S) - 1;
if (argc == 1)
{
SpineAnimation_new* arg0 = (SpineAnimation_new*)tolua_tousertype(tolua_S,2,0);
SpineAnimation_new* ret = SpineAnimation_new::createWithSkeletonAnimation(arg0);
return 1;
}
在lua代码中,我先将所有spine动画创建加载,放在一个table中,然后在游戏过程中,通过键值获取table中预先创建好的spine动画实现创建。
local spineAnim = cc.SpineAnimation_new:createWithFileName("role/tn_zt/tn_zt.json","role/tn_zt/tn_zt.atlas")--预加载动画资源
self._sprite3d = cc.SpineAnimation_new:createWithSkeletonAnimation(spineAnim)--创建动画
self:addChild(self._sprite3d)--添加到当前场景
self._sprite3d:setPosition(200,200)
通过以上方法就可以实现spine动画的预加载
(转载时请注明出处,from 博客园:HemJohn)
标签:
原文地址:http://www.cnblogs.com/HemJohn/p/4800655.html