ok let's see here is everything I do with animations:
loading (for south skeleton/animation):
std::map<Key, spine::SkeletonDrawable*> m_animations;
loadAnimation(THIEF, SOUTH, "data/Thief/Walking South/front");
void TextureManager::loadAnimation(CharacterType type, Direction direction, const std::string& path)
{
Atlas* atlas = Atlas_createFromFile((path + ".atlas").c_str(), 0);
SkeletonJson* json = SkeletonJson_create(atlas);
json->scale = 0.3f;
SkeletonData* data = SkeletonJson_readSkeletonDataFile(json, (path + ".json").c_str());
if (!data)
std::cerr << "Resource Manager: failed to load Animation " << Key(type, direction).toString() << " from \"" << path << "\"!" << std::endl;
else
{
SkeletonJson_dispose(json);
m_animations[Key(type, direction)] = new spine::SkeletonDrawable(data);
}
}
//this is how I get them later
spine::SkeletonDrawable* TextureManager::getAnimation(CharacterType type, Direction direction)
{
Key key(type, direction);
if (m_animations.find(key) == m_animations.end())
std::cerr << "Resource Manager: Animation " << key.toString() << " not found!" << std::endl;
return m_animations[key];
}
update:
Whenever the movingdirection changes I get the new drawable. If the character stops moving the movingdirection is NONE and the viewingdirection selects the drawable and animaiton.
void Thief::updateMovingDirection()
{
m_pDrawable = m_pPlayer->getTexManager()->getAnimation(m_type,
m_movingDirection == NONE ? m_viewingDirection : m_movingDirection);
switch (m_movingDirection)
{
case NORTH:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "running_v4", true);
break;
case EAST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "running_right", true);
break;
case SOUTH:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "running3", true);
break;
case WEST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "running_left", true);
break;
case NORTH_EAST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "running_right", true);
break;
case SOUTH_EAST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "running_right", true);
break;
case SOUTH_WEST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "running_left", true);
break;
case NORTH_WEST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "running_left", true);
break;
case NONE:
switch (m_viewingDirection)
{
case NORTH:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "idle", true);
break;
case EAST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "idle_right", true);
break;
case SOUTH:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "idle", true);
break;
case WEST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "idle_left", true);
break;
case NORTH_EAST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "idle_right", true);
break;
case SOUTH_EAST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "idle_right", true);
break;
case SOUTH_WEST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "idle_left", true);
break;
case NORTH_WEST:
AnimationState_setAnimationByName(m_pDrawable->state, 0, "idle_left", true);
break;
default:
break;
}
break;
default:
break;
}
}
The big switch block is currently needed because all animation are named differently. Once they are all named "idle" or "running" I will replace it with this:
AnimationState_setAnimationByName(m_pDrawable->state, 0, m_movingDirection == NONE ? "idle" : "running", true);
Initially I set the viewingdirection to south and update the movingdirection to NONE. So the south idle animation is played.
m_viewingDirection = SOUTH;
updateMovingDirection(NONE);
Then I update the Skeleton (no matter what):
m_pDrawable->skeleton->x = getPosition().x + 64;
m_pDrawable->skeleton->y = getPosition().y + 128;
m_pDrawable->update(elapsedTime.asSeconds());
Drawing:
m_pDrawable->skeleton->a = m_isInvisible || m_isDead ? 100.f : 1.f; //make him transparent
target.draw(*m_pDrawable, states);
And that's it. I got no clue what could cause a problem...