Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8064923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:30:25+00:00 2026-06-05T11:30:25+00:00

I’m working on a simple 2D top-down Zelda style game in C++, but I’m

  • 0

I’m working on a simple 2D top-down Zelda style game in C++, but I’m having trouble getting multiple instances of an enemy class to spawn in. Whenever I spawn more than one of an enemy, only the first one registers any collision detection; all other enemies seem to be merely visual “ghosts” that are rendered to the screen. When the first enemy dies, the only one that can, then all other “ghosts” disappear along with it.

I’ve created an enemy manager class that uses a vector list to hold active enemies, check each one’s collision against any box passed in, and update/render the enemies.

class cEnemyMgr {
public:
std::vector<cEnemy*> mobList;

cEnemyMgr(){}
~cEnemyMgr(){
    for (int i=0; i < mobList.size(); i++) {
        mobList[i]->texture.Close();
            //delete mobList[i];
    } 
}

    void render() {
        for (int i=0; i < mobList.size(); i++) {
            mobList[i]->render();
        }
    }

    void update(float dt){
        for (int i=0; i < mobList.size(); i++) {
            if ( mobList[i]->hp <= 0 ){
                mobList[i]->die();
                mobList.pop_back();
            } else {
                mobList[i]->update(dt);
            }
        }
    }

    void spawnMob(int x, int y){
        cEnemy* pEnemy = new cMeleeEnemy();
        pEnemy->init(x, y);
        mobList.push_back(pEnemy);
    }

    cEnemy* checkCollisions(int x, int y, int wd, int ht){
        for (int i=0; i < mobList.size(); i++) {
            int left1, left2;
            int right1, right2;
            int top1, top2;
            int bottom1, bottom2;

            left1 = x;
            right1 = x + wd;
            top1 = y;
            bottom1 = y + ht;

            left2 = mobList[i]->pos.x;
            right2 = mobList[i]->pos.x + 64;
            top2 = mobList[i]->pos.y;       
            bottom2 = mobList[i]->pos.y + 64;

            if ( bottom1 < top2 ) { return NULL; }
            if ( top1 > bottom2 ) { return NULL; }
            if ( left1 > right2 ) { return NULL; }
            if ( right1 < left2 ) { return NULL; }

            return mobList[i];
        }
    }
};

The enemy class itself is pretty basic; cEnemy is the base class, from which cMeleeEnemy is derived. It has the standard hp, dmg, and movement variables so that it can crawl around the screen to try and collide with the player’s avatar and also respond to being attacked by the player. All of this works fine, it’s just that when I try to have multiple enemies, only the first one spawned in works correctly while the rest are empty shells, just textures on the screen. It doesn’t matter if I make explicit calls to spawnMob rapidly in the same block or if I space them out dynamically with a timer; the result is the same. Can anyone point me in the right direction?

–EDIT–
Here’s the code the for enemy.h:

#ifndef ENEMY_H
#define ENEMY_H

#include "texture.h"
#include "timer.h"

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

class cEnemy {
public:
int hp;
int dmg;
D3DXVECTOR2 pos;
D3DXVECTOR2 fwd;
D3DXVECTOR2 vel;
D3DCOLOR color;
int speed;
float rotate;
bool hitStun;
float hitTime;
CTexture texture;

virtual void init(int x, int y) = 0;
virtual void update(float dt) = 0;
virtual void die() = 0;

void render(){
    texture.Blit(pos.x, pos.y, color, rotate);
}

void takeDamage(int dmg) {
    if (hitStun == false){
        extern CTimer Timer;        
        hitTime = Timer.GetElapsedTime();
        hp -= dmg;
        color = 0xFFFF0000;
        hitStun = true;
    }
}

void hitStunned(float duration) {
    extern CTimer Timer;
    float elapsedTime = Timer.GetElapsedTime();
    if ( elapsedTime - hitTime > duration ){
        color = 0xFFFFFFFF;
        hitStun = false;
    }
}

};

class cPlayer : public cEnemy {
public:
int facing;

void init(int x, int y);
void update(float dt);
void die();

};

class cMeleeEnemy : public cEnemy {
public:
cMeleeEnemy(){}
~cMeleeEnemy(){
    texture.Close();
}

void init(int x, int y);
void update(float dt);
void die();

};
#endif

And enemy.cpp:

#include "enemy.h"

void cPlayer::update(float dt){
// Player Controls
if ( KEY_DOWN('W') ) {
    pos.y -= speed * dt;
    facing = 0;
} else if( KEY_DOWN('S') ) {
    pos.y += speed * dt;
    facing = 2;
}

if ( KEY_DOWN('A') ) {
    pos.x -= speed * dt;
    facing = 3;
} else if( KEY_DOWN('D') ) {
    pos.x += speed * dt;
    facing = 1;
}

// Hit Recovery
if ( hitStun == true ) {    
    hitStunned(1.0);
}
}

void cMeleeEnemy::update(float dt){
extern cPlayer player1;
extern int ScreenWd;
extern int ScreenHt;

D3DXVECTOR2 dir;
dir = player1.pos - pos;
D3DXVec2Normalize(&dir, &dir);
//fwd = (fwd * 0.2) + (dir * 0.8);
fwd = dir;
vel = vel + fwd * speed * dt;

pos = pos + vel * dt;

//keep em on screen
if ( pos.x < 0 ) { pos.x = 0; }
if ( pos.x > ScreenWd - 64 ) { pos.x = ScreenWd - 64; }
if ( pos.y < 0 ) { pos.y = 0; }
if ( pos.y > ScreenHt - 64 ) { pos.y = ScreenHt - 64; }

// Hit Recovery
if ( hitStun == true ) {    
    hitStunned(0.5);
}

}

void cMeleeEnemy::die(){
extern int score;
extern int numMobs;
score += 1;
numMobs -= 1;
//texture.Close();
}

void cPlayer::die(){
extern char gameState[256];
sprintf(gameState, "GAMEOVER");
}

void cMeleeEnemy::init(int x, int y){
hp = 6;
dmg = 1;
speed = 25;
fwd.x = 1;
fwd.y = 1;
vel.x = 0;
vel.y = 0;
pos.x = x;
pos.y = y;
rotate = 0.0;
color = 0xFFFFFFFF;
hitStun = false;
texture.Init("media/vader.bmp");
}

void cPlayer::init(int x, int y){
facing = 0;
hp = 10;
dmg = 2;
color = 0xFFFFFFFF;
speed = 100;
fwd.x = 1;
fwd.y = 1;
vel.x = 0;
vel.y = 0;
pos.x = x;
pos.y = y;
rotate = 0.0;
hitStun = false;
texture.Init("media/ben.bmp");
}

As you can tell, I’m not that experienced yet. This is my first on-your-own project for school. I just have to say I’m a little confused on where I should be closing textures and deleting objects. Thanks for your time, guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:30:28+00:00Added an answer on June 5, 2026 at 11:30 am

    In your checkCollisions function, you return NULL, or the object at the position of the first index of the enemy vector after every loop.

    Therefore, when the first ghost is not hit, the checkCollisions function will return NULL instead of iterating through each of the subsequent ghosts in the vector.

    To fix this, change your checkCollisions function to the following:

    cEnemy* checkCollisions(int x, int y, int wd, int ht){
        for (int i=0; i < mobList.size(); i++) {
            int left1, left2;
            int right1, right2;
            int top1, top2;
            int bottom1, bottom2;
    
            left1 = x;
            right1 = x + wd;
            top1 = y;
            bottom1 = y + ht;
    
            left2 = mobList[i]->pos.x;
            right2 = mobList[i]->pos.x + 64;
            top2 = mobList[i]->pos.y;       
            bottom2 = mobList[i]->pos.y + 64;
    
            if ( bottom1 < top2 ) { continue; }
            if ( top1 > bottom2 ) { continue; }
            if ( left1 > right2 ) { continue; }
            if ( right1 < left2 ) { continue; }
    
            return mobList[i];
        }
    
        return NULL;
    }
    

    Hope this helps!

    EDIT:

    Note that when you are removing an enemy from the list if it’s HP is 0 or less, you are using mobList.pop_back(), but this removes the final element from the vector, you should use something like the following to remove the enemy you want from the list:

    std::remove_if( mobList.begin(), mobList.end() []( cEnemy* pEnemy )->bool
    {
         if( pEnemy->hp <= 0 )
         {
             pEnemy->die();
             return true;
         }
         else
         {
             pEnemy->update();
             return false;
         }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.