I encoded the game in C ++ using the SDL library. Today, changing the way my character class works, I ran into a very mysterious problem. The following code is part of my logic for allowing a player to fire a bullet. The control variables b_canFire and b_shouldFire (I plan to rename them to make more sense) are set elsewhere in the class so that this function is executed when the user presses a key.
bool PlayerChar::DoFiring() { if(b_canFire && b_shouldFire) { Fire(box.x + 22, box.y); // This fires a bullet b_canFire = false; // Does not work b_shouldFire = false; // Does not work return true; } }
When I go through this code using my debugger, it becomes aparrent that the values โโof b_canFire and b_shouldFire do not change to false by the destination inside the if statement. However, if I changed the code to the following:
bool PlayerChar::DoFiring() { if((b_canFire) && (b_shouldFire)) { Fire(box.x + 22, box.y); // This fires a bullet b_canFire = false; // Works b_shouldFire = false; // Works return true; } }
or
bool PlayerChar::DoFiring() { if(b_canFire == true && b_shouldFire == true) { Fire(box.x + 22, box.y); // This fires a bullet b_canFire = false; // Works b_shouldFire = false; // Works return true; } }
Suddenly the tasks are being completed. I also tried to reproduce this situation in an empty test project as follows:
bool bC = true; bool bD = true; if(bC == true && bD == true) { bC = false; // Works bD = false; // Works } bool bE = true; bool bF = true; if(bE && bF) { bE = false; // Works bF = false; // Works }
However, both of these examples assign values โโexactly as they should. Itโs clear that I missed something, but for life I donโt see what it is. I figured out how to fix the problem in order to get my code to work, but it really bothers me, not knowing what is upsetting in the first example, because everything I learned about C ++ tells me that they should work fine.
This is my first major project using the C ++ language, and I am still participating in it, so any help or advice from more experienced programmers will be great.
Thanks!
EDIT:
As requested, the whole class is presented here:
#include <list> #include "SDL_mixer.h" #include "hiGlobalVars.h" #include "hiGlobalObjects.h" #include "hiAssetManager.h" #include "hiRendering.h" #include "hiTimer.h" #include "hiBullet.h" #include "hiPlayerChar.h" #include "hiDebugger.h" using std::list; PlayerChar::PlayerChar() { moveSpeed = 6; moveDir = NONE; b_canFire = true; b_shouldFire = false; box.x = 0; box.y = 470; box.w = 38; box.h = 40; } PlayerChar::~PlayerChar() { } void PlayerChar::SetPos(int x) { box.x = x; } void PlayerChar::Draw() { BlitSurface(box.x, box.y, assets.ss_playerchar_idle, ss_screen); } bool PlayerChar::DoFiring() { if((b_canFire) && (b_shouldFire)) { // Fire a bullet Fire(box.x + 22, box.y); b_canFire = false; b_shouldFire = false; return true; // fired a bullet } return false; // did not fire a bullet } void PlayerChar::Fire(int x, int y) { // Create a new bullet at the correct location and add it to the global bullet list Bullet* bullet = new Bullet(); bullet->SetPos(x, y); bullets.push_back(bullet); // Play bullet firing sound Mix_PlayChannel(-1, assets.mc_firebullet, 0); } void PlayerChar::HandleInput(Uint8* keystates) { if(keystates[SDLK_LEFT] && keystates[SDLK_RIGHT])// Both direction keys moveDir = NONE; if(keystates[SDLK_LEFT] && !keystates[SDLK_RIGHT]) // Left key and not right key moveDir = LEFT; if(!keystates[SDLK_LEFT] && keystates[SDLK_RIGHT]) // Right key and not left key moveDir = RIGHT; if(!keystates[SDLK_LEFT] && !keystates[SDLK_RIGHT]) // Neither direction key moveDir = NONE; if(keystates[SDLK_SPACE]) // Space bar b_shouldFire = true; if(!keystates[SDLK_SPACE]) // Allow another bullet to be fired after release b_canFire = true; } void PlayerChar::Move() { if(moveDir == LEFT && box.x > 0) // If not off screen, move box.x -= moveSpeed; if(moveDir == RIGHT && box.x < (ss_screen->w - box.w)) box.x += moveSpeed; }
and title:
#ifndef __hiPlayerChar__ #define __hiPlayerChar__ #include "SDL.h" #include "SDL_mixer.h" #include "hiGlobalVars.h" enum MoveDir { LEFT, RIGHT, NONE }; class PlayerChar { private: Sint16 moveSpeed; MoveDir moveDir; bool b_canFire; bool b_shouldFire; public: