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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - TG HellHound

Pages: [1]
1
General / Re: sf::clock not working (or maybe I am being stupid)
« on: August 16, 2012, 11:33:24 pm »
I have got stuck again, yes I am a total noob at this. So basically I want to have 2 loops going off one will be the loop for when my invader is moving left the other when it is moving right. Now my idea was that on the moving down part of each loop ie when the invader hits the side of the screen and moves down I would simply change a bool from being false to true and it would go onto the next loop, then that one at its down bit would change it back. Its not having it, the 2 errors I am getting while playing around with different things is either the sprite will go down and then continue moving the same way, ie not breaking out the loop when the bool changes, or if I force the loop to end with a break my program terminates.

Could you guys help me out again please:

while (movementloop = true){
                bool x ;
       
                if ( x = true)Invader1Sprite.Move(-2 * 0.05,0 );
                if ( x = false)Invader1Sprite.Move(-2 * 0.05, 0);
                float ElapsedTime = clock.GetElapsedTime();
                float ElapsedTime2 = clock2.GetElapsedTime();
             if (ElapsedTime >= 2.0){Invader1Sprite.SetImage(Invader2);}
                 if (ElapsedTime >= 4.0){Invader1Sprite.SetImage(Invader1); clock.Reset();}
                 if (ElapsedTime2 >= 10 ){Invader1Sprite.Move(0, 1 * 0.05); Invader1Sprite.Move(+2 * 0.05, 0);}
                 if (ElapsedTime2 >= 11){ clock2.Reset();movementloop = false; break;}
                 Window.Clear(sf::Color(0, 0, 0));
                Window.Draw(Invader1Sprite);
                Window.Display();}
               
                while (movementloop2=false){
                        bool x ;
                        if ( x = true)Invader1Sprite.Move(-2 * 0.05,0 );
                if ( x = false)Invader1Sprite.Move(2 * 0.05, 0);
                float ElapsedTime = clock.GetElapsedTime();
                float ElapsedTime2 = clock2.GetElapsedTime();
               
             if (ElapsedTime >= 2.0){Invader1Sprite.SetImage(Invader2);}
                 if (ElapsedTime >= 4.0){Invader1Sprite.SetImage(Invader1); clock.Reset();}
                 if (ElapsedTime2 >= 10 ){x=false;Invader1Sprite.Move(0, 1 * 0.05); Invader1Sprite.Move(+2 * 0.05, 0);}
                 if (ElapsedTime2 >= 11){ clock2.Reset();}
                 Window.Clear(sf::Color(0, 0, 0));
                Window.Draw(Invader1Sprite);
                Window.Display();
                movementloop = true;
                }
                }

Disregard all the random X's and things, I have been trying out a lot of different stuff.

2
General / Re: sf::clock not working (or maybe I am being stupid)
« on: August 16, 2012, 06:24:20 pm »
/facepalm Yep I just moved the sf::Clock clock out of the while(Window.IsOpened()) loop and it worked just as I wanted it to. Cheers for your help exploiter, many thanks :D

3
General / Re: sf::clock not working (or maybe I am being stupid)
« on: August 16, 2012, 05:56:09 pm »
The if(Window.IsOpened()) is just there to start the sprite moving without requiring any input. Could you elaborate on what you mean by

If you have the decleration of the clock next to the time request you'll create a new clock object each iteration and the elapsedtime between creation and request will be just a few milli or micro seconds.

And here is all of my code to give you context:

#include <SFML\Graphics.hpp>
#include <iostream>

int main()

{

        sf::VideoMode Vmode(800, 600, 32);
        sf::RenderWindow Window(Vmode, "Invaders");


        sf::Image Invader1;
        if (!Invader1.LoadFromFile("Invader1.png"))
                return 1;

        sf::Sprite Invader1Sprite;
        Invader1Sprite.SetImage(Invader1);
        Invader1Sprite.Move(100,0);
        Invader1Sprite.SetPosition(600,0);

        sf::Image Invader2;
        if (!Invader2.LoadFromFile("Invader2.png"))
                return 1;

       
       
       
        while(Window.IsOpened())
        {
               
               
                sf::Event Event;
                while (Window.GetEvent(Event))
                {

                        switch (Event.Type)

                        {
                        case sf::Event::Closed: Window.Close(); break;
                        default: break;
                        }
                }
                Window.Clear(sf::Color(0, 0, 0));
                Window.Draw(Invader1Sprite);

       
                Window.Display();
                sf::Clock clock;
                float ElapsedTime = clock.GetElapsedTime();

// Move the sprite
                if (Window.IsOpened())  Invader1Sprite.Move(-50 * 0.0005,0 );
               
                if (ElapsedTime >= 3.0) Invader1Sprite.SetImage(Invader2);
       
               
        }

        return 0;
}

4
General / sf::clock not working (or maybe I am being stupid)
« on: August 16, 2012, 05:40:44 pm »
Ok so I am creating a Space Invaders clone and I need to have a sprite that changes between two images. To do this I am using the Clock.GetElapsedTime function to switch to the second image once it hits a certain time, this however is just not working. Could someone maybe point out where I am going wrong, as this code looks fine to me:
sf::Clock clock;
                float ElapsedTime = clock.GetElapsedTime();


                if (Window.IsOpened())  Invader1Sprite.Move(-50 * 0.0005,0 );
               
                if (ElapsedTime >= 3.0) Invader1Sprite.SetImage(Invader2);
       

Pages: [1]
anything