Advertisement Jump to content
Sign in to follow this  

Vector Problem?

This topic is 3114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, so in my program, I set the Ingame (playing interface) class to contain, among other things, a MainChar sprite and a Enemy vector. Both are derived from the same class (CCharacter), and the game initializes the main character and one enemy at the beginning:

MainChar = new CCharacter(96, 96, 60, 15, 15, 10, 0, 0, 5, "Knight.png");
Enemies.push_back(CCharacter(96, 96, 60, 15, 15, 10, 0, 0, 5, "Knight.png"));

The problem, however, is that the program fails to load the animation for the enemy class, which comes up as an error during the rendering phase.

I also went through and debugged the code, and it seems like the enemy initializes all of the animations correctly (WalkLeft, WalkRight, PauseLeft, PauseRight), but gets a bad pointer for the current animation, while MainChar didn't:

void CAnimatedSprite::PlayAnimation(const std::string &strAnimName)
m_iterCurrentAnim = m_mapAnimations.find(strAnimName);
if (m_iterCurrentAnim == m_mapAnimations.end())
std::string strError = "Unable to play: " + strAnimName;
strError += ". Animation not found.";
throw CException(strError);

Nothing is different supposedly between the two classes, so why does the MainChar sprite work while the enemy sprite gets a bad pointer? Is it impossible to make a vector work in this scenario or something(?), and if so, what would be a good substitute?

Share this post

Link to post
Share on other sites
Original post by SiCrane
Does your CCharacter class obey the rule of three?

I don't think it uses any of the three things in that article, but I'm not sure...Here's the class:

class CCharacter : public CAnimatedSprite
CCharacter(int h, int Rw, int w, int HP, int MP, int AT, int DF, int MG, int SP, std::string fileName);

int GetH() {return m_iHeight;};
int GetW() {return m_iWidth;};
float GetXMomentum() {return m_iXMomentum;};
float GetYMomentum() {return m_iYMomentum;};

// Used so that the character doesnt slide
void Stop() {m_iXMomentum = 0;};

// Calculations to make sure the character stays logically existent
void calcFall(std::list<CPlatform> platforms);
void calcJump(std::list<CPlatform> platforms);
void calcMove(int Xoffset, std::list<CWall> walls);

// Change the character's state of animation
void UpdateAnimation(bool m_KeysDown[4]);
// The way our character was last moving
std::string m_strLastDir;

// Vital stats
int m_pHP, m_pMP;
int m_pMaxHP, m_pMaxMP;
int AT, DF, MG, SP;

// Position of the sprite and movement values
int m_iOffset;
int m_iHeight;
int m_iWidth;
float m_iXMomentum;
float m_iYMomentum;

Share this post

Link to post
Share on other sites
Original post by SiCrane
What does CAnimatedSprite look like?

Okay, well I figured out the problem. It just turns out that I misinterpreted the debugger output (hate those things)...My real problem was just when I cycled through the vector and tried to draw each instance. So it all works now...but if you want to see how the Animated Sprite class looks anyway,
Here it is:

class CAnimatedSprite
// Default constructor and destructor.
CAnimatedSprite(): m_iXPos(0), m_iYPos(0) {};
~CAnimatedSprite() {};

// Adds a new animation for the sprite. The strAnimName
// is a string that identifies the animation and should
// be unique for this sprite.
void AddAnimation(const std::string& strAnimName,
const CImageList& lstAnimation);
// Plays a previously loaded animation. The strAnimName
// is the name that was passed when calling AddAnimation.
void PlayAnimation(const std::string& strAnimName);

// Draw the current frame of the animation at the sprite
// current position.
void DrawSprite(int xOffset, int yOffset);
// Go to the next animation frame.
void NextFrame();

// Set the position of the sprite
void SetPosition(int XPos, int YPos)
m_iXPos = XPos;
m_iYPos = YPos;
// Move the sprite from its current position
void OffsetPosition(int XOffset, int YOffset)
m_iXPos += XOffset;
m_iYPos += YOffset;
int GetX() {return m_iXPos;};
int GetY() {return m_iYPos;};
typedef std::map<std::string, CImageList> TAnimMap;
typedef TAnimMap::iterator TAnimMapIter;

// Map containing all the animations that can be
// played.
TAnimMap m_mapAnimations;
// Iterator to the current animation being played
TAnimMapIter m_iterCurrentAnim;
// Position of the sprite
int m_iXPos;
int m_iYPos;

// If the sprite should go to the next frame (frame rate was too fast)
bool advFrame;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!