I’m trying to add offsets for arm positions to the sprite frames used in my character.
I have this inherited from CCSpriteFrame:
class CharacterFrame : public cocos2d::CCSpriteFrame
{
public:
///@brief The frame's number
unsigned int m_frameNum;
///@brief The arm's offset (if the character has seperate arms).
cocos2d::CCPoint m_armOffset;
};
Then I use this code to access the frame later:
CharacterFrame* frame = (CharacterFrame*)m_sprite->displayFrame();
if ( frame )
{
CCPoint fpos = frame->m_armOffset;
m_arms->setPosition(fpos);
}
The problem is that the offset positions don’t get stored. I know I set them correctly for each frame.
CCAnimation* ccAnim = CCAnimation::create();
unsigned int frameCount = 0;
//Load each frame
tinyxml2::XMLElement* firstAnim = element->FirstChildElement();
tinyxml2::XMLElement* nextAnim = firstAnim;
while ( nextAnim!=NULL )
{
if ( std::string(nextAnim->Name()) == std::string("frame") )
{
frameCount++;
std::string filepath = dir;
filepath.append("/");
filepath.append(nextAnim->Attribute("file"));
//Create frame
CCImage* img = new CCImage();
img->initWithImageFile(filepath.c_str());
CCTexture2D* tex = new CCTexture2D();
tex->initWithImage(img);
CharacterFrame* frame = new CharacterFrame();
CCSize sz = tex->getContentSize();
frame->initWithTexture(tex, CCRect(0, 0, sz.width, sz.height));
frame->m_frameNum = frameCount;
float offsX = nextAnim->FloatAttribute("armOffsetX");
float offsY = nextAnim->FloatAttribute("armOffsetY");
frame->m_armOffset = ccp(offsX, offsY);
ccAnim->addSpriteFrame(frame);
loadFrame(nextAnim, animName, frameCount);
}
nextAnim = nextAnim->NextSiblingElement();
}
//Turn on looping
ccAnim->setRestoreOriginalFrame(anim.m_loop);
//Calculate animation speed
ccAnim->setDelayPerUnit(anim.m_duration/(float)frameCount);
//Add animation to structure
ccAnim->retain();
anim.m_animation = ccAnim;
What is it that I’m missing?