SFML community forums
Help => General => Topic started 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
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 :(
-
- 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=.
-
it still doesn't work :( and whats the diffence between calling it with set image, would i use get image to display it?
-
it still doesn't work
You must restart the clock everytime you change the image.
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, ...).
-
thanks well i changed the code to
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?
-
Can you post some more code? Specifically where you draw your graphic to the screen.
You should be doing a "myWindow.Draw(myImage);" somewhere....
-
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
}
-
Your draw function should be:
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!
}
:)
-
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
-
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?
-
even when i put a constant value no movement and my x n y co-ordinates are
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?
-
It seems you create a new Clock on every update? Oo
void SpaceInvadersDemo::update()
{
sf::Clock Clock;
if (Clock.GetElapsedTime()>=Timestep)
{
shipSprite.SetImage(shipImage2);
s.setPosX(s.getPosX()+moveIncrement);
Clock.Reset();
}
void SpaceInvadersDemo::play()
{
-
:shock: :shock: :shock: OMG how could i miss that lol it now moves :D thank you
-
My first (and helpfull) post :D