坐标的问题
按照之前的修改之后,发现在代码里面加入了新的子节点,坐标全不对了。查看CreatorReader.cpp代码发现有个adjustPosition的函数,实现很简单,这里不做叙述,然后将其导入lua中,发现子节点添加坐标没有问题了。这里注意是在代码里面新建然后加入读出来的creator文件中会出的问题。
同理,在导出animotion给lua使用的时候,发现坐标同样出了问题,于是,修改AnimateClip.h文件,添加3个函数,当然这里可以把3个合并成一个。
void adjustPosition(cocos2d::Node* node, cocos2d::Vec2 nextPos) const;
void AnimateClip::adjustPositionY(cocos2d::Node* node, float nextValue) const;
void AnimateClip::adjustPositionX(cocos2d::Node* node, float nextValue) const;
然后在.cpp文件中实现:
void AnimateClip::adjustPosition(cocos2d::Node* node, cocos2d::Vec2 nextPos) const
{
const cocos2d::Node* parent = node->getParent();
// only adjust position if there is a parent, and the parent is no the root scene
if (parent && dynamic_cast<const cocos2d::Scene*>(parent) == nullptr) {
const auto p_ap = parent->getAnchorPoint();
const auto p_cs = parent->getContentSize();
const auto offset = cocos2d::Vec2(p_ap.x * p_cs.width, p_ap.y * p_cs.height);
nextPos = nextPos + offset;
node->setPosition(nextPos);
}
}
void AnimateClip::adjustPositionX(cocos2d::Node* node, float nextValue) const
{
const cocos2d::Node* parent = node->getParent();
if (parent && dynamic_cast<const cocos2d::Scene*>(parent) == nullptr)
{
const auto p_ap = parent->getAnchorPoint();
const auto p_cs = parent->getContentSize();
const auto offset = cocos2d::Vec2(p_ap.x * p_cs.width, p_ap.y * p_cs.height);
nextValue = nextValue + offset.x;
node->setPositionX(nextValue);
}
}
void AnimateClip::adjustPositionY(cocos2d::Node* node, float nextValue) const
{
const cocos2d::Node* parent = node->getParent();
if (parent && dynamic_cast<const cocos2d::Scene*>(parent) == nullptr)
{
const auto p_ap = parent->getAnchorPoint();
const auto p_cs = parent->getContentSize();
const auto offset = cocos2d::Vec2(p_ap.x * p_cs.width, p_ap.y * p_cs.height);
nextValue = nextValue + offset.y;
node->setPositionY(nextValue);
}
}
//修改doUpdate函数
if (getNextValue(animProperties.animPosition, elapsed, nextPos))
{
adjustPosition(target, nextPos);
}
// positoin x
if (getNextValue(animProperties.animPositionX, elapsed, nextValue))
adjustPositionX(target, nextValue);
// position y
if (getNextValue(animProperties.animPositionY, elapsed, nextValue))
//target->setPositionY(nextValue);
adjustPositionY(target, nextValue);
这里原理和之前的adjust一样。这样导出来的动画播放就没有问题了。
creator animotion 帧动画的实现
comps的导出的确比较麻烦,帧动画实现需要用到SpriteFrame,但是这个版本的creator_to_cocos2dx并没有实现。我们项目又急需这个功能,那么做如下修改,只是临时的,等待cocos2dx 官方的comps支持。
首先修改Node.js
//添加如下函数
function parseCurDataComps(comps){
function addComps(form,form_key,to,to_key) {
// debugger;
if (form[form_key]) {
to[to_key] = [];
form[form_key].forEach(function(comps){
let path_info = Utils.get_sprite_frame_name_by_uuid(comps.value.__uuid__);
let value = {
frame: comps.frame,
texturePath: path_info,
};
to[to_key].push(value);
});
}
}
let result = {};
if (comps['cc.Sprite']) {
// debugger;
let spriteComps = comps['cc.Sprite']
addComps(spriteComps,'spriteFrame',result,'spriteFrame');
//other will do
}
//debugger;
//other will do
return result;
}
然后做如下修改
接着修改convert_fire_to_json.py 把这里注释掉
修改CreatorReader.fbs文件:
table AnimationClip
{
// clip name
name:string;
duration:float;
sample:float;
speed:float;
wrapMode:AnimWrapMode;
curveData:[AnimCurveData];
}
table AnimCurveData
{
path:string;
props:AnimProps;
comps:AnimComps;
}
table AnimProps
{
rotation:[AnimPropRotation];
position:[AnimPropPosition];
positionX:[AnimPropPositionX];
positionY:[AnimPropPositionY];
anchorX:[AnimPropAnchorX];
anchorY:[AnimPropAnchorY];
color:[AnimPropColor];
opacity:[AnimPropOpacity];
width:[AnimPropWidth];
height:[AnimPropHeight];
scaleX:[AnimPropScaleX];
scaleY:[AnimPropScaleY];
skewX:[AnimPropSkewX];
skewY:[AnimPropSkewY];
}
table AnimComps
{
spriteFrame:[AnimCompsSpriteFrame];
}
table AnimCompsSpriteFrame
{
frame:float;
texturePath:string;
}
这样就可以导出SpriteFrame了,然后再在c++进行添加支持 就可以了。