I am trying to jump my simple sprite and i have written some code but every time when i press up key which is for jumping the sprite sprite jumps after few seconds , is there any solution here is my code please help as soon..Thanks in advance.
using namespace std;
int main()
{
sf::VideoMode Vmode1(600,400,32);
sf::RenderWindow Window1(Vmode1, "Project Aasra");
Window1.SetFramerateLimit(60);
Window1.UseVerticalSync(true);
//SECTION START: Images and Sprites
double x,y,dx, dy;
x=y=dx=dy=0;
sf::Image mario2;
if(!mario2.LoadFromFile("images/mario2.png")){return EXIT_FAILURE; }
sf::Sprite mario2_sprite(mario2);
bool hold =true;
while(hold)
{
sf::Event Event1;
bool space_down=false;
while(Window1.GetEvent(Event1))
{
//window controls
if(Event1.Type==sf::Event::Closed) { Window1.Close(); hold=false; }
}
//SECTION START: direction keys press
if(Window1.GetInput().IsKeyDown(sf::Key::Right)) dx+=0.5;
if(Window1.GetInput().IsKeyDown(sf::Key::Left)) dx-=0.5;
mario2_sprite.SetPosition(x,y);
x+=dx;
y+=dy;
/*friction*/
dx*=0.775;
/*jump*/
if((Window1.GetInput().IsKeyDown(sf::Key::Up)))
{
dy=-4.0;
space_down=true;
}
if (!Window1.GetInput().IsKeyDown(sf::Key::Up)) space_down=false;
/*gravity*/
if (mario2_sprite.GetPosition().y<252) dy+=0.1;
/*Screen Limit*/
if((mario2_sprite.GetPosition().y + mario2_sprite.GetSize().y) > 252)
{
mario2_sprite.SetY(252);
}
Window1.Clear(sf::Color(0,255,255));
Window1.Draw(mario2_sprite);
Window1.Display();
}
}