Page 1 of 1

How to properly use IAnimationEndCallBack

Posted: Wed Apr 18, 2007 5:31 pm
by spopo
Hello again, i came with new problems once more. Right now it's with class IAnimationEndCallBack usage. So, let's start :
i have class Enemy, which has private (it could be public) members that shows which animation is currently being played, and which not.

Code: Select all


struct anim {
	bool run;
	bool attack;
};

class Enemy {
	                ..............
	anim EnemyAnim;
	                .............
};
And what i do not know, is how to implement the IAnimationEndCallBack class into the Enemy class, so that every Enemy class would have it's own IAnimationEndCallBack class, which would change the Enemy class members, for example : run animation has ended - EnemyAnim.run = false;

And one more thing : in the main.cpp i'd only have to create an instance of Enemy class. Any help will be appreciated ! :wink:

Posted: Thu Apr 19, 2007 11:56 am
by spopo
Anyone there? Or are there any ways to avoid using this class? Are there any other ways to see what animation has ended?

Posted: Thu Apr 19, 2007 9:28 pm
by monkeycracks
Mind showing how you solved? I've been curious of this myself.

Posted: Fri Apr 20, 2007 5:19 am
by spopo
Sure.

Enemy.h

Code: Select all

struct anim {
        bool run;
        bool attack;
};
 
class Enemy;
 
class AnimCallback : public irr::scene::IAnimationEndCallBack
{
public:
    void OnAnimationEnd(irr::scene::IAnimatedMeshSceneNode* node);
 
    void setEnemy (Enemy *enemy);
    
private:
    Enemy *m_Enemy;
};
 
class Enemy {
        irr::scene::IAnimatedMeshSceneNode *Node;
        anim EnemyAnim;
        AnimCallback  Callback;
public:
         anim* getAnim(void);    	
         void attack (void);
};
Enemy.cpp

Code: Select all


void Enemy::attack (void)
{
	EnemyAnim.attack = true;
	Node->setMD2Animation(irr::scene::EMAT_ATTACK);
	Node->setAnimationEndCallback(&Callback);
}	


anim* Enemy::getAnim(void)
 {
       return &EnemyAnim;
 }


void AnimCallback::OnAnimationEnd(irr::scene::IAnimatedMeshSceneNode* node)
{
       if (m_Enemy->getAnim()->run) m_Enemy->getAnim()->run = false;	   
       if (m_Enemy->getAnim()->attack) m_Enemy->getAnim()->attack = false;
}

void AnimCallback::setEnemy (Enemy *enemy)
 {
        m_Enemy = enemy;
 }

// And in the Enemy constructor 

Callback.setEnemy (this);


Posted: Fri Apr 20, 2007 7:30 am
by vitek
It seems that you could simplify things just a tad by refactoring and making the enemy respond to its on animation end callback...

Code: Select all

class Enemy : private irr::scene::IAnimationEndCallBack
{ 
private:
  irr::scene::IAnimatedMeshSceneNode *Node; 
private:
  virtual void OnAnimationEnd(irr::scene::IAnimatedMeshSceneNode* node); 

public: 
  void attack (void); 
}; 
 
void Enemy::attack (void) 
{ 
  //... snip ...

  Node->setMD2Animation(irr::scene::EMAT_ATTACK); 
  Node->setAnimationEndCallback(this); 
}    

void Enemy::OnAnimationEnd(irr::scene::IAnimatedMeshSceneNode* node) 
{
  //... snip ...
} 

Posted: Fri Apr 20, 2007 10:24 am
by spopo
To vitek: Node->setAnimationEndCallback(this);

gives

error C2664: 'irr::scene::IAnimatedMeshSceneNode::setAnimationEndCallback' : cannot convert parameter 1 from 'Enemy *const ' to 'irr::scene::IAnimationEndCallBack *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cas

Posted: Fri Apr 20, 2007 10:46 am
by monkeycracks
Ahh thanks. I've always wondered how to use this.

Posted: Fri Apr 20, 2007 3:18 pm
by hybrid
@spopo: Maybe you did not drive it correctly, or you made the attack method const (which would make this const also)?

Posted: Sun Apr 22, 2007 3:34 pm
by spopo
Nevermind :oops:

Posted: Mon Mar 17, 2008 3:13 pm
by doqkhanh