Hi
I apologise for my ignorance with using SFML, I have been using it for 2 weeks as I want to learn a new piece of software with my dissertation.
My aim is to create a timer which monitors the time that has elapsed since its last reset. The user will need to click an enemy to increase their score before the timer has reached five seconds. If the timer reaches five seconds then set the enemy position to another random place on the screen.
I am using an enum to determine what the game state is currently in, whether it is in 'Playing', 'Exiting' or 'Uninitialised' which determines what is happening in the game as the entire game works around one main function 'gameLoop'.
My problem is if I use cout to display the elapsed time it displays the time like 0.000843478 and doesn't appear to increase, it appears to be a random sequence of numbers that never reaches 0.001.
Any ideas? This is my gameLoop function:
void GameClass::gameLoop()
{
sf::Event currentEvent; //Creates an instance of sf Event called currentEvent which will monitor the gameState whilst the program is running
gameWindow.GetEvent(currentEvent); //Assigns the currentEvent to gameWindow
switch(gameState) //Monitors the changes within gameState and applies the appropriate implementation depending on the cases below
{
case GameClass::Playing: //Alter the game environment so that all sprites are drawn to stage ready for the user to play
{
sf::Clock enemyClock;
bool enemyHit = false;
while((enemyClock.GetElapsedTime()<5.f)&&(enemyHit == false))
{
gameWindow.Clear(sf::Color(0,0,0)); //Clears all images off screen and reverts it to a black screen
enemy.drawCharacter(gameWindow);
player.drawCharacter(gameWindow);
gameWindow.Display();
cout<<"Time: "<<enemyClock.GetElapsedTime()<<endl;
if(currentEvent.Type == sf::Event::Closed)
{
gameState = GameClass::Exiting; //If the 'X' button is closed on the gameWindow then the gameState changes to Exiting
}
switch(currentEvent.Type)
{
case sf::Event::MouseButtonPressed:
switch (currentEvent.MouseButton.Button)
{
case sf::Mouse::Left:
{
player.setIsFiringStatus(true);
player.fireWeapon();
//Debug to test if enemy is being hit
int enemyX = enemy.enemySprite.GetPosition().x;
int enemyY = enemy.enemySprite.GetPosition().y;
int playerX = player.userCursor.GetPosition().x;
int playerY = player.userCursor.GetPosition().y;
if(((((enemyX - playerX)>0)&&(enemyX - playerX)<30)&&((enemyY - playerY)>0)&&(enemyY - playerY)<30)||((((playerX - enemyX)>0)&&(playerX - enemyX)<30)&&(((playerY - enemyY)>0)&&(playerY - enemyY)<30)))
{
enemyHit = true;
cout<<"Enemy hit"<<endl;
break;
//enemy.setEnemyPosition();
}
player.setIsFiringStatus(false);
break;
}
default:
break;
}
break;
case sf::Event::MouseButtonReleased:
//cout<<"Mouse button released"<<endl;
break;
case sf::Event::MouseMoved:
{
//Need to update the position of the mouse/cursor sprite
player.userCursor.SetPosition((currentEvent.MouseMove.X), (currentEvent.MouseMove.Y));
break;
}
default:
break;
}
break;
}
if(enemyHit)
{
player.addToUserScore(100);
cout<<"Score: "<<player.getUserScore()<<endl;
enemy.setEnemyPosition();
//enemyClock.Reset();
}
/*if(enemyClock.GetElapsedTime()>5.f)
{
enemy.setEnemyPosition();
}*/
}
default: //Prevents warnings
break;
}
}