Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Clock::GetElapsedTime() error in my code  (Read 4188 times)

0 Members and 1 Guest are viewing this topic.

melissa_goddard

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • http://melissagoddard.deviantart.com
Clock::GetElapsedTime() error in my code
« on: January 07, 2012, 10:35:20 pm »
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:

Code: [Select]

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;
}
}
Kind Regards

Melissa

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Clock::GetElapsedTime() error in my code
« Reply #1 on: January 08, 2012, 04:07:33 am »
You seem to be creating a new sf::Clock each loop, causing it to restart the time.
Create it outside the loop (as class member for example) or an unclean solution would be making it static sf::Clock enemyClock (but correct me if I'm wrong!)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Clock::GetElapsedTime() error in my code
« Reply #2 on: January 08, 2012, 09:47:01 am »
And read the doc / tutorials about events, you're using them in a wrong way ;)
Laurent Gomila - SFML developer

melissa_goddard

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • http://melissagoddard.deviantart.com
Clock::GetElapsedTime() error in my code
« Reply #3 on: January 08, 2012, 09:52:44 am »
Ok that's great I'll do both of those thanks very much!
Kind Regards

Melissa