Hello
i'm trying to make an image to move in a specific direction. for example i want it to first move right, if it reach 50 pixel it stops and then move down, if it reach 50 pixel again it moves left and so on. but the problem is when it try to move left it can't, it just start to shake and not move.
here is the code:
#include <SFML\Graphics.hpp>
#include <iostream>
class mov
{
public:
mov();
~mov();
void drawmap(sf::RenderWindow &window);
private:
sf::Image grassimage, dirtimage;
sf::Sprite grasssprite, dirstsprite;
};
#include "mov.h"
mov::mov()
{
dirtimage.LoadFromFile("dirt.png");
dirstsprite.SetPosition(0,0);
}
mov::~mov()
{
}
void mov::drawmap(sf::RenderWindow &window)
{
float ElapsedTime = window.GetFrameTime();
dirstsprite.SetImage(dirtimage);
int posX = dirstsprite.GetPosition().x;
int posY = dirstsprite.GetPosition().y;
int Speed = 8;
int Up = -10;
int Down = 10;
int Right = 10;
int Left = -10;
dirstsprite.Move(Right * ElapsedTime * Speed, 0);
if(posX >=50)
dirstsprite.Move( -(Right * ElapsedTime * Speed), Down * ElapsedTime * Speed);
if(posY >=50)
dirstsprite.Move( Left * ElapsedTime * Speed, -(Down * ElapsedTime * Speed));
window.Draw(dirstsprite);
}
#include <SFML\Graphics.hpp>
#include "mov.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(800,600,32),"AI Follow Grass Road");
mov SM;
while(window.IsOpened())
{
window.SetFramerateLimit(60);
sf::Event Event;
while(window.GetEvent(Event))
{
}
window.Clear();
SM.drawmap(window);
window.Display();
}
return 0;
}