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 8739485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:56:48+00:00 2026-06-13T10:56:48+00:00

I’ve been coding a game in C++ using the SDL library. Today while changing

  • 0

I’ve been coding a game in C++ using the SDL library. Today while changing the way my player character class works, I’ve come up against a very puzzling problem. The following code forms part of my logic allowing the player to fire bullets. The control variables, b_canFire and b_shouldFire (I plan to rename these to make more sense), are set elsewhere in the class to allow this function to execute 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 step through this code using my debugger, it becomes aparrent that the values of b_canFire and b_shouldFire are not being changed to false by the assignments inside the if statement. However, if I change 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 assignments work. I have also tried replicating 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 the values exactly as they should. Clearly I am missing something here, but for the life of me, I can’t see what it is. I have figured out how to fix the problem to make my code work, but it’s really bothering me not knowing what is breaking the assignments in the first example, because everything I’ve learned about C++ so far is telling me they should work fine.

This is my first major project using the C++ language, and I am still learning, so any help or advice from more experienced programmers would be great.

Thanks!

EDIT:

As requested here is the entire class:

#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 the header:

#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:
    // Ctr & Dtr
    PlayerChar();
    ~PlayerChar();

    // Functions
    void SetPos(int x);
    bool DoFiring();
    void Fire(int x, int y);
    void Move();
    void Draw();
    void HandleInput(Uint8* keystates);

    // Size and position
    SDL_Rect box;
};


#endif
  • 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-13T10:56:49+00:00Added an answer on June 13, 2026 at 10:56 am

    It is missing last return instruction in your function:

    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;
        }
        return false; // WAS MISSING HERE
    }
    

    Then your function returns whatever (UB) if any of your b_canFire, b_shouldFire are false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
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.