Reputation: 123
I have an EnemyBullet subclass of superclass Bullet. Now I'm calling the Bullet method Process() using an EnemyBullet object. What I want is to determine whether the current object is an EnemyBullet to distinguish from Bullet action. My code is like this,
void Bullet::Process(float deltaTime)
{
// Generic position update, based upon velocity (and time).
m_y = m_y + this->GetVerticalVelocity()*deltaTime;
m_pSprite->SetY(static_cast<int>(m_y));
m_pSprite->SetX(static_cast<int>(m_x));
if (m_y < 0)
{
SetDead(true);
}
//Here I want to detect current object is an EnemyBullet, then define a different action
//I tried the following code, but it didn't work
if (EnemyBullet* v = static_cast<EnemyBullet*>(Bullet)) {
if (m_y >800)
{
SetDead(true);
}
}
}
Upvotes: 0
Views: 204
Reputation:
Here's an example of calling a method on an instance of a subclass from a method in the superclass:
class Bullet {
public:
void process() {
// update m_y...
// update sprite position...
if (this->isDead()) {
// handle dead bullet...
}
}
virtual bool isDead() {
return (m_y < 0);
}
protected:
int m_y;
};
class EnemyBullet : public Bullet {
public:
bool isDead() override {
return (m_y > 800);
}
};
Note how each bullet type has custom isDead
logic.
Upvotes: 1