My solution based on cocos2dx:
void
CCSpineNode::slotChangeDisplay(const char* slotName, const char* imgPath) {
if (!_spSkeleton || !slotName || !imgPath)
return;
spSlot* slot = spSkeleton_findSlot(_spSkeleton, slotName);
if (!slot)
return;
spAttachment* attach = slot->attachment;
if (!attach)
return;
spAttachmentType type = attach->type;
spRegionAttachment* attachReg = NULL;
spMeshAttachment* attachMesh = NULL;
std::string regionName = "";
if (type == SP_ATTACHMENT_REGION) {
attachReg = SUB_CAST(spRegionAttachment, attach);
spAtlasRegion* reg = (spAtlasRegion*)attachReg->rendererObject;
regionName = reg->name;
}
else if (type == SP_ATTACHMENT_MESH) {
attachMesh = SUB_CAST(spMeshAttachment, attach);
spAtlasRegion* reg = (spAtlasRegion*)attachMesh->rendererObject;
regionName = reg->name;
}
else
return;
std::string name = "";
std::string path = "";
char buff[1024];
memset(buff, 0, sizeof(buff));
strcpy(buff, imgPath);
int i = strlen(buff);
while (buff[i] != '/') {
buff[i] = '\0';
i
---
;
}
path = buff;
name = imgPath;
name = name.substr(path.length(), name.length()-path.length());
memset(buff, 0, sizeof(buff));
for (int i = 0, c = name.length(); i < c; i++) {
if (name[i] == '.')
break;
buff[i] = name[i];
}
name = buff;
CCSpineManager* mgr = CCSpineManager::sharedSpineManager();
spAtlas* atlas = mgr->addAtlasWithImage(name.c_str(), path.c_str(),
regionName.c_str());
if (!atlas)
return;
spAtlasRegion* atlasReg = spAtlas_findRegion(atlas, regionName.c_str());
if (!atlasReg)
return;
if (type == SP_ATTACHMENT_REGION) {
attachReg->rendererObject = atlasReg;
attachReg->regionOffsetX = atlasReg->offsetX;
attachReg->regionOffsetY = atlasReg->offsetY;
attachReg->regionWidth = atlasReg->width;
attachReg->regionHeight = atlasReg->height;
attachReg->regionOriginalWidth = atlasReg->originalWidth;
attachReg->regionOriginalHeight = atlasReg->originalHeight;
spRegionAttachment_setUVs(attachReg,
atlasReg->u,
atlasReg->v,
atlasReg->u2,
atlasReg->v2,
atlasReg->rotate);
}
else if (type == SP_ATTACHMENT_MESH) {
attachMesh->rendererObject = atlasReg;
attachMesh->regionU = atlasReg->u;
attachMesh->regionV = atlasReg->v;
attachMesh->regionU2 = atlasReg->u2;
attachMesh->regionV2 = atlasReg->v2;
attachMesh->regionRotate = atlasReg->rotate;
attachMesh->regionOffsetX = atlasReg->offsetX;
attachMesh->regionOffsetY = atlasReg->offsetY;
attachMesh->regionWidth = atlasReg->width;
attachMesh->regionHeight = atlasReg->height;
attachMesh->regionOriginalWidth = atlasReg->originalWidth;
attachMesh->regionOriginalHeight = atlasReg->originalHeight;
spMeshAttachment_updateUVs(attachMesh);
}
}
addAtlasWithImage: create a atlas use memory data
you do not need to create a new attachment, just change the rendererObject and uv info of the attachment