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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:38:47+00:00 2026-06-02T03:38:47+00:00

Earlier i moved some data to a singleton object, GamePropertiesManager. In this class i

  • 0

Earlier i moved some data to a singleton object, GamePropertiesManager. In this class i define a variable called _gameSpeed and in the class constructor i use an intialisation list to set it to 1 as default.

In another class i have an update loop that, (outside of my control), will always update as frequently as possible. I intended to, and successfully used a ‘speed’ variable to slow down the updating. This worked by, say, setting it to 2 the game would only update every other call to onUpdate.. I might make sense if you look at the contents of PlayState::onUpdate() below (I posted the whole cpp under the advice of a commenter):

#include "PlayState.hpp"
#include "PlayerController.hpp"
#include "AIController.hpp"

#include <stdlib.h>
#include <time.h>

//testin
#include <iostream>

/// Implement PlayState

PlayState::PlayState()
{}
PlayState::~PlayState()
{}

bool PlayState::onCreate()
{
   _gameProperties = GamePropertiesManager::GetInstance();

   _frameCounter = 0;
   application.addKeyListener( *this );
   _collisionFlag = false;

   //snakes created, intialising a pointer to fruit managaer, controller and opposing snake
   _player1 = new Snake();
   _player2 = new Snake();
   _player1->Initialise(&_fruitManager, new PlayerController(_player1), _player2);
   _player2->Initialise(&_fruitManager, new AIController(_player2), _player1);

   return true;
}
bool PlayState::onDestroy()
{
   return true;
}

void PlayState::onEntry()
{
   //seed to randomise food positions later
    srand ( time(0) );
}

void PlayState::onExit()
{

}

void PlayState::onUpdate()
{
   //TODO: rename _snake speed and maybe make it global gamespeed
   ++_frameCounter; //FrameCounter can be used to slow the snake down
   if(_frameCounter >= _gameProperties->GetGameSpeed())
   {

       //updating
       _fruitManager.UpdateFruits();
      _player1->Update(&_collisionFlag);
      _player2->Update(&_collisionFlag);

      if (_collisionFlag)
         application.setState("gameover");

      _frameCounter = 0;
   }

}

void PlayState::onRender( Canvas& c )
{
   _player1->Draw(c);
   _player2->Draw(c);

    _fruitManager.DrawFruits(c);
}

bool PlayState::onKey (const KeyEvent& key)
{
if( key.key_state == KeyEvent::KB_DOWN ){

    switch (key.key){
         case KeyEvent::KB_ESC_KEY:
            application.exit();
            break;
        case 'p':
            application.setState("pause");
            break;
      }
}
   return true;
}

_gameProperties is a pointer to a/the GamePropertiesManager which i assign in PlayState::onCreate(), when my class PlayState is first created..

The actual GamePropertiesManager class looks like this:

#include "GamePropertiesManager.hpp"

GamePropertiesManager* GamePropertiesManager::_instance = NULL;

GamePropertiesManager* GamePropertiesManager::GetInstance()
{
   if (!_instance) //instance not yet created
      _instance = new GamePropertiesManager();

   return _instance;
}

void GamePropertiesManager::Destroy()
{
   delete _instance;
   _instance = 0;
}

GamePropertiesManager::GamePropertiesManager() :
   _gameMode(PLAYERVSCOM), _player1Name("Player 1"), _player2Name("Player 2"),
      _player1Score(0), _player2Score(0), _matchSurvivor("NONE"), _gameSpeed(1)
{

}

unsigned int GamePropertiesManager::GetGameSpeed()
{
   return _gameSpeed;
}

My problem is that, when i change the value of _gameSpeed in the initialisation list to any value aside from (1) my program crashes at runtime. I find this particularly odd as i was previously comparing _frameCounter to both hard coded values other than 1 and a speed variable set to values other than 1 and the program ran fine as expected. Any ideas about the problem?

Hope i explained myself well enough, appreciate your help!

  • 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-02T03:38:48+00:00Added an answer on June 2, 2026 at 3:38 am

    The reason is almost certainly because for any value other than 1, in the first call to PlayState::onUpdate the if check won’t run, most likely leaving something uninitialized.

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

Sidebar

Related Questions

earlier I use Spring MVC and annotation @ModelAttribute. @Controller public class ArticleController { @ModelAttribute(articles)
Recently i moved my project from .Net3.5 to .Net4. In earlier version to use
Earlier I asked this question , which basically asked how to list 10 winners
Earlier I had a problem when an inner anonymous class did not see a
I asked this question earlier. I am intrigued by std::set but I have another
I asked a question earlier about why left joins in Linq can't use defined
I moved my Android project to a laptop and now I get this when
Earlier I asked this question which was on how to make my own set
[This is an updated version of a question posted earlier, the previous title was
I asked a question earlier about which language to use for an AI prototype.

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.