Hi All,
I need to achieve something similar: I'm using cocos2dx and I want to add a CCSprite on top of a bone (I don't have z-order issues in-between the parts of my skeleton, I want the sprite always visible).
I've read this thread and I almost managed to achieve this by manually setting my sprite position using the worldX/Y of the bone during the update event. Then I set the rotation (the addition of the worldRotation of the bone, and the rotation of the skeleton itself).
But I have a slight issue, if you look closely, you'll see that the sprite is not exactly "glued" to the head, you can see it slightly moving apart from the head bone.
Here's a modified version of ExampleLayer.cpp from the cocos2dx example from the spine-runtime repository:
#include "ExampleLayer.h"
#include <iostream>
#include <fstream>
#include <string.h>
using namespace cocos2d;
using namespace spine;
using namespace std;
CCScene* ExampleLayer::scene () {
CCScene *scene = CCScene::create();
scene->addChild(ExampleLayer::create());
return scene;
}
static CCSprite* sprite;
bool ExampleLayer::init () {
if (!CCLayer::init()) return false;
skeletonNode = CCSkeletonAnimation::createWithFile("spineboy.json", "spineboy.atlas");
skeletonNode->setMix("walk", "jump", 0.2f);
skeletonNode->setMix("jump", "walk", 0.4f);
skeletonNode->setAnimation("walk", true);
skeletonNode->timeScale = 0.3f;
skeletonNode->debugBones = true;
skeletonNode->runAction(CCRepeatForever::create(CCSequence::create(
CCMoveBy::create(2.0f,ccp(200.0f,0.0f)),
CCMoveBy::create(2.0f,ccp(-200.0f,0.0f)),
CCRotateBy::create(2.0f,90),
CCRotateBy::create(2.0f,-90),
NULL)));
CCSize windowSize = CCDirector::sharedDirector()->getWinSize();
skeletonNode->setPosition(ccp(windowSize.width / 2, 20));
addChild(skeletonNode,1);
sprite = CCSprite::create("sprite.png");
CC_SAFE_RETAIN(sprite);
addChild(sprite,2);
scheduleUpdate();
return true;
}
void ExampleLayer::update (float deltaTime) {
//NO EFFECT: skeletonNode->updateWorldTransform();
Bone* head = skeletonNode->findBone("head");
CCPoint pos = ccpAdd(ccp(head->worldX,head->worldY),ccp(30,0));
pos = skeletonNode->convertToWorldSpace(pos);
sprite->setPosition(pos);
sprite->setRotation(skeletonNode->getRotation()+head->worldRotation);
}
Just use any "sprite.png" file, it doesn't matter (well, not something too big or too small, something like 100x100 is fine). You can change my "30,0" value to offset the sprite to have a better reference (like having the sprite aligned with the head debug bone, and in this case you'll see the debug bone line being overlapped sometimes by the sprite)
I've tried to updateWorldTransform just before getting the bone with no effect.
Any idea?
Thanks for any help anoyone can provide!