SFML community forums

Help => General => Topic started by: jamku on July 19, 2011, 06:33:29 pm

Title: how to display alternate images at each timestep
Post by: jamku on July 19, 2011, 06:33:29 pm
hey im tryin to make a program where the sprite has two images which are displayed alternatively at each timestep to give the impression of movement like space invaders


i did this  
Code: [Select]

    if (Clock.GetElapsedTime()==Timestep)
{

shipSprite=sf::Sprite(shipImage2);
s.setPosX(s.getPosX()+moveIncrement);

}


the timestep can be modified so a gamedesigner could change the speed of movement but it doesnt move at all :(
Title: how to display alternate images at each timestep
Post by: Nexus on July 19, 2011, 06:53:42 pm
Title: how to display alternate images at each timestep
Post by: jamku on July 19, 2011, 07:13:46 pm
it still doesn't work :( and whats the diffence between calling it with set image, would i use get image to display it?
Title: how to display alternate images at each timestep
Post by: Laurent on July 19, 2011, 07:17:00 pm
Quote
it still doesn't work

You must restart the clock everytime you change the image.

Quote
whats the diffence between calling it with set image

With SetImage you only change the image.
With operator= you completely overwrite your previous sprite with a new one (so you reset the position, subrect, rotation, color, ...).
Title: how to display alternate images at each timestep
Post by: jamku on July 21, 2011, 05:28:15 pm
thanks well i changed the code to
Code: [Select]
if (Clock.GetElapsedTime()>=Timestep)
{

shipSprite.SetImage(shipImage2);
s.setPosX(s.getPosX()+moveIncrement);
Clock.Reset();


}

and it still doesn't move does anybody know what the problem could be?
Title: how to display alternate images at each timestep
Post by: slotdev on July 21, 2011, 06:14:03 pm
Can you post some more code? Specifically where you draw your graphic to the screen.

You should be doing a "myWindow.Draw(myImage);" somewhere....
Title: how to display alternate images at each timestep
Post by: jamku on July 21, 2011, 06:31:25 pm
Code: [Select]
void SpaceInvadersDemo::init()
{
shipImage.LoadFromFile("myC++/images/asprite1.png");
shipImage2.LoadFromFile("myC++/images/asprite2.png");
shipSprite=sf::Sprite(shipImage), (shipImage2);
shipSprite.SetCenter(shipSprite.GetSize().x/2,shipSprite.GetSize().y/2);
s.setRadius(shipSprite.GetSize().x/2);
s.setPosX(width/2);s.setPosY(height/10);



}
void SpaceInvadersDemo::draw()
{
win.Clear(sf::Color(0,0,0));
shipSprite.SetPosition(s.getPosX(),s.getPosY());
win.Draw(shipSprite);
}
void SpaceInvadersDemo::getInput()
{
sf::Event Event;
        while (win.GetEvent(Event))
        {

            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                win.Close();
           //Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                win.Close();


        }
}
void SpaceInvadersDemo::update()
{
sf::Clock Clock;


if (Clock.GetElapsedTime()>=Timestep)
{

shipSprite.SetImage(shipImage2);
s.setPosX(s.getPosX()+moveIncrement);
Clock.Reset();


}

void SpaceInvadersDemo::play()
{
//the game loop
draw();//the offscreen buffer
getInput();//from the user
update();//integrate user input and any game movement which is automatic
}

Title: how to display alternate images at each timestep
Post by: slotdev on July 21, 2011, 06:38:53 pm
Your draw function should be:

Code: [Select]

void SpaceInvadersDemo::draw()
{
   win.Clear(sf::Color(0,0,0));
   shipSprite.SetPosition(s.getPosX(),s.getPosY());
   win.Draw(shipSprite);
  win.Display(); // display the new frame!
}


:)
Title: how to display alternate images at each timestep
Post by: jamku on July 21, 2011, 07:57:06 pm
it still aint moving my guess is the clock aint increasing so it aint reaching higher or equal 2 the timestep is there away i can keep track of the clock? so when it hits a certain time value it will excute the if statement
Title: how to display alternate images at each timestep
Post by: slotdev on July 22, 2011, 03:34:24 pm
I suggest you remove Timestep for now, and just replace it with a constant value, maybe 100 or something.

Also check what the x and y positions for your sprite are - are they negative or crazy values?
Title: how to display alternate images at each timestep
Post by: jamku on July 22, 2011, 08:53:14 pm
even when i put a constant value no movement and my x n y co-ordinates are
Code: [Select]
s.setPosX(width/2);s.setPosY(height/10);

but why would that be the problem, does the if statement call for the elapsed time consecutively until it matches the expression? or does it only call once?
Title: how to display alternate images at each timestep
Post by: Teemperor on July 22, 2011, 10:18:38 pm
It seems you create a new Clock on every update?  Oo

Code: [Select]
void SpaceInvadersDemo::update()
{
   sf::Clock Clock;
   

   if (Clock.GetElapsedTime()>=Timestep)
   {
       
      shipSprite.SetImage(shipImage2);
      s.setPosX(s.getPosX()+moveIncrement);
      Clock.Reset();
       
       
   }

void SpaceInvadersDemo::play()
{
Title: how to display alternate images at each timestep
Post by: jamku on July 22, 2011, 11:00:47 pm
:shock:  :shock:  :shock: OMG how could i miss that lol it now moves :D thank you
Title: how to display alternate images at each timestep
Post by: Teemperor on July 23, 2011, 10:27:20 am
My first (and helpfull) post :D