How can I make my Mario jump and then fall back down using this code:
#include<SFML/Graphics.hpp>
#include<iostream>
int main()
{
enum Direction { Right };
sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "SpriteSheet Animation");
sf::Texture texture;
sf::Texture tex;
sf::Texture up;
sf::Sprite sprite;
sf::Sprite still;
sf::Sprite jump;
sf::Vector2i source(1, Right);
if(!texture.loadFromFile("Data/Running.gif"))
std::cout << "could not locate the specified file" << std::endl;
else
sprite.setTexture(texture);
if(!tex.loadFromFile("Data/Standing.gif"))
std::cout << "could not locate the specified file" << std::endl;
else
still.setTexture(tex);
if(!up.loadFromFile("Data/Jumping.gif"))
std::cout << "could not locate the specified file" << std::endl;
else
jump.setTexture(up);
sprite.setPosition(0,400);
jump.setPosition(0,-400);
still.setPosition(0,400);
while(Window.isOpen())
{
sf::Event Event;
while(Window.pollEvent(Event))
{
if(Event.type == sf::Event::Closed || Event.key.code == sf::Keyboard::Escape)
Window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
sprite.move(.1,0);
sprite.setTextureRect(sf::IntRect(source.x * 15, source.y * 20, 15, 20));
still.setPosition(-50,-50);
jump.setPosition(-50,-50);
}
if (Event.type == sf::Event::KeyReleased && Event.key.code == sf::Keyboard::Right) {
still.setPosition(sprite.getPosition().x,sprite.getPosition().y);
sprite.setTextureRect(sf::IntRect(source.x * 0, source.y * 0, 0, 0));
jump.setPosition(-50,-50);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
sprite.setTextureRect(sf::IntRect(source.x * 0, source.y * 0, 0, 0));
still.setPosition(-50,-50);
jump.setPosition(sprite.getPosition().x,sprite.getPosition().y);
}
if (sprite.getPosition().x < 0 )
{
sprite.setPosition(0, 400);
}
if (sprite.getPosition().x > 786)
{
sprite.setPosition(0, 400);
}
source.x++;
if(source.x * 15 >= texture.getSize().x)
source.x = 0;
Window.clear();
Window.draw(still);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
Window.draw(sprite);
}
Window.draw(jump);
Window.display();
}
return 0;
}