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

Author Topic: how to display alternate images at each timestep  (Read 4616 times)

0 Members and 1 Guest are viewing this topic.

jamku

  • Newbie
  • *
  • Posts: 7
    • View Profile
how to display alternate images at each timestep
« 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 :(

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
how to display alternate images at each timestep
« Reply #1 on: July 19, 2011, 06:53:42 pm »
  • Use >= rather than ==. It is unlikely that your time is hit exactly.
  • To assign a new image, call sf::Sprite::SetImage() instead of sf::Sprite::operator=.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

jamku

  • Newbie
  • *
  • Posts: 7
    • View Profile
how to display alternate images at each timestep
« Reply #2 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
how to display alternate images at each timestep
« Reply #3 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, ...).
Laurent Gomila - SFML developer

jamku

  • Newbie
  • *
  • Posts: 7
    • View Profile
how to display alternate images at each timestep
« Reply #4 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?

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
how to display alternate images at each timestep
« Reply #5 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....
SFML 2.1

jamku

  • Newbie
  • *
  • Posts: 7
    • View Profile
how to display alternate images at each timestep
« Reply #6 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
}


slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
how to display alternate images at each timestep
« Reply #7 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!
}


:)
SFML 2.1

jamku

  • Newbie
  • *
  • Posts: 7
    • View Profile
how to display alternate images at each timestep
« Reply #8 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

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
how to display alternate images at each timestep
« Reply #9 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?
SFML 2.1

jamku

  • Newbie
  • *
  • Posts: 7
    • View Profile
how to display alternate images at each timestep
« Reply #10 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?

Teemperor

  • Newbie
  • *
  • Posts: 6
    • View Profile
how to display alternate images at each timestep
« Reply #11 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()
{

jamku

  • Newbie
  • *
  • Posts: 7
    • View Profile
how to display alternate images at each timestep
« Reply #12 on: July 22, 2011, 11:00:47 pm »
:shock:  :shock:  :shock: OMG how could i miss that lol it now moves :D thank you

Teemperor

  • Newbie
  • *
  • Posts: 6
    • View Profile
how to display alternate images at each timestep
« Reply #13 on: July 23, 2011, 10:27:20 am »
My first (and helpfull) post :D