I have a cocos2d-x project and I have a requirement to attach objects (other sprites) to an existing animation. consider I have sprite animation of human transferred into a monster. and want to place something in his hand, I would like that each frame I will change the location of the other object based on the frame. the coordinates which relate to the hand will be hand-crafted.
I managed to achieve this. but my approach seems to me like an overkill. I would like to know if there is a better way.
What I did :
on my scene, I registered to a schedule :
this->schedule(schedule_selector(LevelScene::updateFrame));
on my updateFrame function I am checking if the animation is playing and if it is I check the animation frame based on the SpriteFrame:
SpriteFrame* spriteFrame = sprite->getSpriteFrame();
int imageIndex = 0;
for (int i = 0; i < animation->getFrames().size(); i++) {
const Vector<AnimationFrame*>& frames = animation->getFrames();
if (spriteFrame == frames.at(i)->getSpriteFrame()) {
imageIndex = i;
break;
}
}
when I find the frame number, I now use a static array which holds the Vectors for this frame and applies to the connected sprite.
That seems to me like a complex solution (but it is working). Hopefully, someone has a better approach.