3.x能用的spine换皮肤方法

贡献一个修改至论坛里另外一个朋友写的动态换皮肤的代码。 有需要的拿去吧

原帖的代码经过COCOS2DX 的API 变化不能用了,这个是修正版。

原帖地址:http://www.cocoachina.com/bbs/read.php?tid-287468-fpage-60-page-2.html 第11楼

JS使用方法 :

_self.role_hero.skeletonAnim.changeAttachment(“face_1”,“face”,“res/role_anim/boy/player.atlas”);

bool SkeletonRenderer::changeAttachment(const std::string& slotName, const std::string& p_w_uploadName, const std::string& atlasFile)
{
spSlot * slot = this->findSlot(slotName);
if (NULL == slot)
{
return false;
}

int nType = slot->p_w_upload->type;
spAtlas* atlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
CCASSERT(atlas, “SkeletonRenderer::changeAttachment Error reading atlas file”);

spAtlasAttachmentLoader* atlasAttachMentLoader = spAtlasAttachmentLoader_create(atlas);
spAttachmentLoader *p_w_uploadLoader = &(atlasAttachMentLoader->super);

spSkin skin = spSkin_create(“default”);
switch (nType)
{
case SP_ATTACHMENT_REGION:
{
spRegionAttachment
regionAttachmentSrc = (spRegionAttachment*)(slot->p_w_upload);
spAttachment* p_w_upload = spAttachmentLoader_newAttachment(
p_w_uploadLoader, skin, SP_ATTACHMENT_REGION, slotName.c_str(), p_w_uploadName.c_str());
//spAttachment* p_w_upload = spAttachmentLoader_newAttachment(p_w_uploadLoader, NULL, SP_ATTACHMENT_REGION, “role_weapon”, “weapon_104005”);
spRegionAttachment* regionAttachment = (spRegionAttachment*)p_w_upload;
regionAttachment->width = regionAttachmentSrc->width;
regionAttachment->height = regionAttachmentSrc->height;
regionAttachment->rotation = regionAttachmentSrc->rotation;
regionAttachment->x = regionAttachmentSrc->x;
regionAttachment->y = regionAttachmentSrc->y;
regionAttachment->scaleX = regionAttachmentSrc->scaleX;
regionAttachment->scaleY = regionAttachmentSrc->scaleY;
regionAttachment->a = regionAttachmentSrc->a;
regionAttachment->b = regionAttachmentSrc->b;
regionAttachment->r = regionAttachmentSrc->r;
regionAttachment->g = regionAttachmentSrc->g;

spRegionAttachment_updateOffset(regionAttachment);
slot->p_w_upload = (spAttachment*)regionAttachment;
}
break;
case SP_ATTACHMENT_BOUNDING_BOX:
{
spBoundingBoxAttachment* boxAttachmentSrc = (spBoundingBoxAttachment*)(slot->p_w_upload);
spAttachment* p_w_upload = spAttachmentLoader_newAttachment(
p_w_uploadLoader, skin, SP_ATTACHMENT_REGION, slotName.c_str(), p_w_uploadName.c_str());
spBoundingBoxAttachment* boxAttachment = (spBoundingBoxAttachment*)p_w_upload;
boxAttachment->verticesCount = boxAttachmentSrc->verticesCount;
boxAttachment->vertices = boxAttachmentSrc->vertices;
for (int i = 0; i < boxAttachmentSrc->verticesCount; i++)
boxAttachment->vertices =boxAttachmentSrc->vertices;
slot->p_w_upload = (spAttachment*)boxAttachment;
}
break;
case SP_ATTACHMENT_MESH:
{
spMeshAttachment* meshAttachmentSrc = (spMeshAttachment*)(slot->p_w_upload);
spAttachment* p_w_upload = spAttachmentLoader_newAttachment(
p_w_uploadLoader, skin, SP_ATTACHMENT_REGION, slotName.c_str(), p_w_uploadName.c_str());
spMeshAttachment* meshAttachment = (spMeshAttachment*)p_w_upload;
meshAttachment->rendererObject = meshAttachmentSrc->rendererObject;
meshAttachment->regionU = meshAttachmentSrc->regionU;
meshAttachment->regionV = meshAttachmentSrc->regionV;
meshAttachment->regionU2 = meshAttachmentSrc->regionU2;
meshAttachment->regionV2 = meshAttachmentSrc->regionV2;
meshAttachment->regionRotate = meshAttachmentSrc->regionRotate;
meshAttachment->regionOffsetX = meshAttachmentSrc->regionOffsetX;
meshAttachment->regionOffsetY = meshAttachmentSrc->regionOffsetY;
meshAttachment->regionWidth = meshAttachmentSrc->regionWidth;
meshAttachment->regionHeight = meshAttachmentSrc->regionHeight;
meshAttachment->regionOriginalWidth = meshAttachmentSrc->regionOriginalWidth;
meshAttachment->regionOriginalHeight = meshAttachmentSrc->regionOriginalHeight;

spMeshAttachment_updateUVs(meshAttachment);
slot->p_w_upload = (spAttachment*)meshAttachment;
}
break;
case SP_ATTACHMENT_SKINNED_MESH:
{
spSkinnedMeshAttachment* skinnedAttachmentSrc = (spSkinnedMeshAttachment*)(slot->p_w_upload);
spAttachment* p_w_upload = spAttachmentLoader_newAttachment(
p_w_uploadLoader, skin, SP_ATTACHMENT_REGION, slotName.c_str(), p_w_uploadName.c_str());
spSkinnedMeshAttachment* skinnedAttachment = (spSkinnedMeshAttachment*)p_w_upload;
skinnedAttachment->rendererObject = skinnedAttachmentSrc->rendererObject;
skinnedAttachment->regionU = skinnedAttachmentSrc->regionU;
skinnedAttachment->regionV = skinnedAttachmentSrc->regionV;
skinnedAttachment->regionU2 = skinnedAttachmentSrc->regionU2;
skinnedAttachment->regionV2 = skinnedAttachmentSrc->regionV2;
skinnedAttachment->regionRotate = skinnedAttachmentSrc->regionRotate;
skinnedAttachment->regionOffsetX = skinnedAttachmentSrc->regionOffsetX;
skinnedAttachment->regionOffsetY = skinnedAttachmentSrc->regionOffsetY;
skinnedAttachment->regionWidth = skinnedAttachmentSrc->regionWidth;
skinnedAttachment->regionHeight = skinnedAttachmentSrc->regionHeight;
skinnedAttachment->regionOriginalWidth = skinnedAttachmentSrc->regionOriginalWidth;
skinnedAttachment->regionOriginalHeight = skinnedAttachmentSrc->regionOriginalHeight;

spSkinnedMeshAttachment_updateUVs(skinnedAttachment);
slot->p_w_upload = (spAttachment*)skinnedAttachment;
}
break;
default:
break;
}
return true;
}

自己绑定到JS里就可以用啦