I am trying to create a player that jumps and moves left and right.
At the moment, the player jumps infinitely on the spot, or moves infinitely left or right when the corresponding keys are pressed.
What do I need to add to this code to make the character stop jumping or moving once the key is released?
Here is the code so far:
void Character::update()
{
b2Vec2 pos = m_body->GetPosition();
setPosition(pos.x, pos.y);
float angle = m_body->GetAngle()* RAD2DEG;
setRotation(angle);
if (m_movingStates[LEFT])
{
m_body->ApplyLinearImpulseToCenter(b2Vec2(-0.03, 0.0f), true);
}
if (m_movingStates[RIGHT])
{
m_body->ApplyLinearImpulseToCenter(b2Vec2(0.03, 0.0f), true);
}
if (m_movingStates[JUMP] && m_sit)
{
m_body->ApplyLinearImpulseToCenter(b2Vec2(0.f, -0.1f), true);
}
}
void Character::onKeyPress(sf::Event event)
{
if (event.key.code == sf::Keyboard::Left)m_movingStates[LEFT] = true;
if (event.key.code == sf::Keyboard::Right)m_movingStates[RIGHT] = true;
if (event.key.code == sf::Keyboard::Space)m_movingStates[JUMP] = true;
}
void Character::onKeyRelease(sf::Event event)
{
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Left) m_movingStates[LEFT] = false;
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Right) m_movingStates[RIGHT] = false;
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space) m_movingStates[JUMP] = false;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I actually posted one similar question but without any non working code. Now i've got some. So i want to make snake move in one direction(ex: UP) until i press the other button(DOWN, LEFT, RIGHT) where i would change direction to that.
However, i've got this piece of code where i commented out one of the problems, but now it only works partially in this manner:
If i press UP i can only press DOWN once more and than it keeps moving forever, if i press LEFT or RIGHT i can only press UP or DOWN and then alternate so DOWN or UP and it keeps moving in that direction forever
Here is the code:
void Game::ProcessEvents(){
sf::Event event;
while (myWindow.pollEvent(event)){
switch(event.type){
case sf::Event::KeyPressed : handlePlayerInput(event.key.code, true); break;
//case sf::Event::KeyReleased : handlePlayerInput(event.key.code, false); break;
case sf::Event::Closed : myWindow.close(); break;
}
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed){
if (key == sf::Keyboard::W || key == sf::Keyboard::Up) movingUp = isPressed;
else if (key == sf::Keyboard::S || key == sf::Keyboard::Down) movingDown = isPressed;
else if (key == sf::Keyboard::A || key == sf::Keyboard::Left) movingLeft = isPressed;
else if (key == sf::Keyboard::D || key == sf::Keyboard::Right) movingRight = isPressed;
}
void Game::update(){
sf::Vector2f movement(0.0f, 0.0f);
if (movingUp) { movingLeft = false; movingRight = false; movingDown = false; movement.y -= 0.1f;}
if (movingDown) { movingUp = false; movingLeft = false; movingRight = false; movement.y += 0.1f; }
if (movingLeft) { movingRight = false; movingUp = false; movingDown = false; movement.x -= 0.1f; }
if (movingRight) { movingLeft = false; movingDown = false; movingUp = false; movement.x += 0.1f; }
isColliding(movement);
}
Oof I'm not the biggest fan of how you're reading inputs and then transforming that to your movement object. For example, if movingUp is true, when will it ever be set to false? The answer is NEVER! There is no way for that to happen!
Instead, I'd make an enum with a single variable for the current direction of movement:
enum Dir { RIGHT, LEFT, UP, DOWN };
Dir curr_dir = RIGHT; // Start the game moving right
Now your handlePlayerInput logic can look like:
if (key == sf::Keyboard::W || key == sf::Keyboard::Up) curr_dir = UP;
else if (key == sf::Keyboard::S || key == sf::Keyboard::Down) curr_dir = DOWN;
else if (key == sf::Keyboard::A || key == sf::Keyboard::Left) curr_dir = LEFT;
else if (key == sf::Keyboard::D || key == sf::Keyboard::Right) curr_dir = RIGHT;
And the update logic can now be:
switch(curr_dir) {
case UP:
movement.y -= 0.1f;
break;
case DOWN:
movement.y += 0.1f;
break;
case RIGHT:
movement.x -= 0.1f;
break;
case LEFT:
movement.x += 0.1f;
break;
}
This is how I would do it:
Instead of having multiple bools have a single variable that denotes the direction. It can be an enum.
enum class Dir { up, down, left, right };
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed){
if (key == sf::Keyboard::W || key == sf::Keyboard::Up) direction = Dir::up;
else if (key == sf::Keyboard::S || key == sf::Keyboard::Down) direction = Dir::down;
else if (key == sf::Keyboard::A || key == sf::Keyboard::Left) direction = Dir::left;
else if (key == sf::Keyboard::D || key == sf::Keyboard::Right) direction = Dir::right;
Then use that variable to compute the movement
void Game::update(){
sf::Vector2f movement(0.0f, 0.0f);
if (direction == Dir::up) movement.y -= 0.1f;
else if (direction == Dir::down) movement += 0.1f;
else if (direction == Dir::left) movement.x -= 0.1f;
else if (direction == Dir::right) movement += 0.1f;
isColliding(movement);
Ok so I have a program that has a void that opens a new window then draws a image on that window every time I call the void. It will let me open a second window but won't let me open it again till I close the second window. I think I am thinking about it wrong if I am can someone give me a better way to do it. Here's my code I don't think its 100% necessary but idk.
My goal is to get it to allow me to open multiple windows doing their own thing on the screen. I am trying to open a new window by calling the DrawKey void.
It is opening a window when I press a key the first time but it is limiting me to the main window and one key window. I want to be able to have as many key windows open as I feel the need for.
class MakeKey
{
public:
typedef struct KeySprite {
sf::Image Img;
sf::Texture Tex;
sf::Sprite Sprite;
}Key;
static bool setTransparency(HWND hwnd, unsigned char alpha) { ... }
static bool setShape(HWND hwnd, const sf::Image& image) { ... }
static sf::Vector2i RandSpawn(sf::Image image) { ... }
static void MakeTopWindow(HWND hwnd) { ... }
static void DrawKey(string key)
{
const unsigned char opacity = 1000;
Key Key;
Key.Img.loadFromFile("Assets/Images/A.png");
sf::RenderWindow window(sf::VideoMode(Key.Img.getSize().x, Key.Img.getSize().y, 32), "Key", sf::Style::None);
setTransparency(window.getSystemHandle(), opacity);
if (key == "A")
Key.Img.loadFromFile("Assets/Images/A.png");
else if (key == "D")
Key.Img.loadFromFile("Assets/Images/D.png");
else if (key == "E")
Key.Img.loadFromFile("Assets/Images/E.png");
else if (key == "Q")
Key.Img.loadFromFile("Assets/Images/Q.png");
else if (key == "S")
Key.Img.loadFromFile("Assets/Images/S.png");
else if (key == "W")
Key.Img.loadFromFile("Assets/Images/W.png");
else if (key == "X")
Key.Img.loadFromFile("Assets/Images/X.png");
else if (key == "Z")
Key.Img.loadFromFile("Assets/Images/Z.png");
else if (key == "Esc")
Key.Img.loadFromFile("Assets/Images/esc.png");
setShape(window.getSystemHandle(), Key.Img);
Key.Tex.loadFromImage(Key.Img);
Key.Sprite.setTexture(Key.Tex);
window.setPosition(MakeKey::RandSpawn(Key.Img));
while (window.isOpen())
{
MakeTopWindow(window.getSystemHandle());
window.clear(sf::Color::Transparent);
window.draw(Key.Sprite);
window.display();
}
}
};
Main
int main()
{
sf::RenderWindow window(sf::VideoMode(100, 100, 32), "Main Window", sf::Style::None);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
MakeKey::MakeTopWindow(window.getSystemHandle());
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::A)
MakeKey::DrawKey("A");
else if (event.key.code == sf::Keyboard::D)
MakeKey::DrawKey("D");
else if (event.key.code == sf::Keyboard::E)
MakeKey::DrawKey("E");
else if (event.key.code == sf::Keyboard::Q)
MakeKey::DrawKey("Q");
else if (event.key.code == sf::Keyboard::S)
MakeKey::DrawKey("S");
else if (event.key.code == sf::Keyboard::W)
MakeKey::DrawKey("W");
else if (event.key.code == sf::Keyboard::X)
MakeKey::DrawKey("X");
else if (event.key.code == sf::Keyboard::Z)
MakeKey::DrawKey("Z");
else if (event.key.code == sf::Keyboard::Escape)
MakeKey::DrawKey("Esc");
}
if (event.type == sf::Event::Closed)
window.close();
}
MakeKey::MakeTopWindow(window.getSystemHandle());
window.clear(sf::Color::Transparent);
window.display();
}
return EXIT_SUCCESS;
}
There is no error just no doing quite what I want and I want help figuring out how to make it do what I want.
So my only goal here is to move the sprite left and right, and while the sprite is moving, I want to switch between two different parts of my sprite sheet every half a second.
I've tried everything I could think of so I left it working and without only the implementation of the needed feature. (EDIT: I've changed it in an attempt to try to follow advice given by another user) I've looked online, but nothing I could find answered my question as far as I could tell. I'm sure there's something I'm overlooking and that it is quite simple, but that's why I need your input.
The third parts of the sprite sheet I want to use is at x = 800, animations facing left are on top at y = 0, and the animations facing right are on bottom at y = 600
Here is the sprite sheet I am using
The first ones are for standing still, and the second two on either row are the ones I want to switch between while "walking"
At any rate, here is my code:
#include "pch.h"
#include <iostream>
#include "SFML/Graphics.hpp"
#include <random>
#include <unordered_map>
enum State
{
Walking,
Standing
};
enum Direction
{
Left,
Right
};
int main(int argc, char ** argv)
{
/* Gregory */
sf::Texture GregorySpriteSheet_T;
GregorySpriteSheet_T.loadFromFile("Images/GregorySpriteSheet.png");
sf::IntRect GregorySpriteRect(0, 600, 400, 600);
sf::Sprite Gregory(GregorySpriteSheet_T, GregorySpriteRect);
sf::RenderWindow renderWindow(sf::VideoMode(1600,800), "SFML 2 Demo");
sf::Event event;
sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Clock deltaClock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
State isWalking{ Standing };
sf::Clock walkClock;
Direction direction = Right;
/* RENDER WINDOW LOOP */
while (renderWindow.isOpen())
{
sf::Time deltaTime = deltaClock.restart();
timeSinceLastUpdate += deltaTime;
while (timeSinceLastUpdate >= timePerFrame)
{
timeSinceLastUpdate -= timePerFrame;
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
renderWindow.close();
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Walking;
direction = Right;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Standing;
}
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Walking;
direction = Left;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Standing;
}
}
}
if (isWalking == Walking)
{
if (direction == Right)
GregorySpriteRect.top = 600;
if (direction == Left)
GregorySpriteRect.top = 0;
if (GregorySpriteRect.left == 0)
GregorySpriteRect.left = 400;
if ((int(walkClock.getElapsedTime().asSeconds() / 1.5f) % 2) == 1)
{
if (GregorySpriteRect.left == 400)
GregorySpriteRect.left == 800;
if (GregorySpriteRect.left == 800)
GregorySpriteRect.left == 400;
walkClock.restart();
}
}
{
using kb = sf::Keyboard;
if (kb::isKeyPressed(kb::Right))
{
Gregory.move(400 * timePerFrame.asSeconds(), 0.0f);
direction = Right;
isWalking = Walking;
}
if (kb::isKeyPressed(kb::Left))
{
Gregory.move(-400 * timePerFrame.asSeconds(), 0.0f);
direction = Left;
isWalking = Walking;
}
if (kb::isKeyPressed(kb::Right) && kb::isKeyPressed(kb::Left))
{
isWalking = Standing;
}
}
if (isWalking == Standing)
{
GregorySpriteRect.left = 0;
if (direction == Right)
GregorySpriteRect.top = 600;
if (direction == Left)
GregorySpriteRect.top = 0;
}
}
Gregory.setTextureRect(GregorySpriteRect);
renderWindow.clear();
renderWindow.draw(Gregory);
renderWindow.display();
}
/* END RENDER WINDOW LOOP */
}
Here is one suggestion, but there are plenty of alternatives:
First, rewrite your event handling slightly such that you can differentiate between the first time a key is held and subsequent times. A simple way is to have an enum State { Standing, Walking } and set it to the appropriate value in your key handler (eg "not walking and key is held? set state to walking")
Then, when the player starts walking, (re)start a "walk" clock.
Finally, when you are actually rendering your player, check this clock and divide the expired time since walking by your walking period (this determines how long one of the walking frames should be held). If there were an even number of such walking periods, use the left walking sprite, otherwise the right walking sprite.
This technique easily generalizes to larger sets of walking frames, of course.
So for anybody that winds up here in the future, I was missing a vital line:
Gregory.setTextureRect(GregorySpriteRect);
Either in my "if" statement for the clock under the "if (isWalking == Walking)", or under it (like I've done here). Pasted the full code below. Works for both directions now. Not sure how optimal this is but if you're an amateur like me this should help.
Also mind that I've reverted back some of the changes suggested from Botje's answer. Although I must credit him for helping me very much with the recommendation of using enum's for Walking and Standing, as well as direction. I also used a ">" operator rather than the "==" operator as suggested by my professor due to rounding errors of float point variables.
Cheers!
#include "pch.h"
#include <iostream>
#include "SFML/Graphics.hpp"
#include <random>
#include <unordered_map>
enum State
{
Walking,
Standing
};
enum Direction
{
Left,
Right
};
int main(int argc, char ** argv)
{
/* Gregory */
sf::Texture GregorySpriteSheet_T;
GregorySpriteSheet_T.loadFromFile("Images/GregorySpriteSheet.png");
sf::IntRect GregorySpriteRect(0, 600, 400, 600);
sf::Sprite Gregory(GregorySpriteSheet_T, GregorySpriteRect);
sf::RenderWindow renderWindow(sf::VideoMode(1600,800), "SFML 2 Demo");
sf::Event event;
sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Clock deltaClock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
State isWalking{ Standing };
sf::Clock walkClock;
Direction direction = Right;
/* RENDER WINDOW LOOP */
while (renderWindow.isOpen())
{
sf::Time deltaTime = deltaClock.restart();
timeSinceLastUpdate += deltaTime;
while (timeSinceLastUpdate >= timePerFrame)
{
timeSinceLastUpdate -= timePerFrame;
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
renderWindow.close();
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Walking;
direction = Right;
GregorySpriteRect.top = 600;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Right)
{
isWalking = Standing;
}
}
if (event.type == sf::Event::EventType::KeyPressed)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Walking;
direction = Left;
GregorySpriteRect.top = 0;
}
}
if (event.type == sf::Event::EventType::KeyReleased)
{
if (event.key.code == sf::Keyboard::Left)
{
isWalking = Standing;
}
}
}
if (isWalking == Walking)
{
if (direction == Right)
GregorySpriteRect.top = 600;
else if (direction == Left)
GregorySpriteRect.top = 0;
else
GregorySpriteRect.top = 600;
if (GregorySpriteRect.left == 0)
GregorySpriteRect.left = 400;
if (walkClock.getElapsedTime().asSeconds() > 0.5f)
{
if (GregorySpriteRect.left == 400)
GregorySpriteRect.left = 800;
else if (GregorySpriteRect.left == 800)
GregorySpriteRect.left = 400;
else
GregorySpriteRect.left += 0;
walkClock.restart();
}
Gregory.setTextureRect(GregorySpriteRect);
}
{
using kb = sf::Keyboard;
if (kb::isKeyPressed(kb::Right))
{
Gregory.move(400 * timePerFrame.asSeconds(), 0.0f);
direction = Right;
}
if (kb::isKeyPressed(kb::Left))
{
Gregory.move(-400 * timePerFrame.asSeconds(), 0.0f);
direction = Left;
}
if (kb::isKeyPressed(kb::Right) && kb::isKeyPressed(kb::Left))
{
isWalking = Standing;
direction = Right;
}
}
if (isWalking == Standing)
{
GregorySpriteRect.left = 0;
if (direction == Right)
GregorySpriteRect.top = 600;
if (direction == Left)
GregorySpriteRect.top = 0;
}
}
Gregory.setTextureRect(GregorySpriteRect);
renderWindow.clear();
renderWindow.draw(Gregory);
renderWindow.display();
}
/* END RENDER WINDOW LOOP */
}
int main() {
sf::RenderWindow window;
sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width / 2) - 445, (sf::VideoMode::getDesktopMode().height / 2) - 480);
window.create(sf::VideoMode(900, 900), "SFML Game", sf::Style::Titlebar | sf::Style::Close);
window.setPosition(centerWindow);
window.setKeyRepeatEnabled(true);
sf::Texture wallTxture;
sf::Sprite wall;
if (!wallTxture.loadFromFile("wall.png")) {
std::cerr << "Error\n";
}
wall.setTexture(wallTxture);
//Gravity Vars:
int groundHeight = 750;
bool isJumping = false;
//Movement Vars:
bool goingRight = false;
bool goingLeft = false;
//Set View Mode:
sf::View followPlayer;
followPlayer.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
Player player("newPlayer.png");
player.setPos({ 800, 800 });
sf::Vector2f position(window.getSize().x / 2, window.getSize().y / 2);
//Main Loop:
while (window.isOpen()) {
const float moveSpeed = 0.1;
sf::Event Event;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
isJumping = true;
//Handle Movement While Jumping:
if (goingLeft == true) {
player.move({ -moveSpeed, -moveSpeed });
}
else if (goingRight == true) {
player.move({ moveSpeed, -moveSpeed });
}
else {
player.move({ 0, -moveSpeed });
}
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
player.move({ 0, moveSpeed });
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
player.move({ -moveSpeed, 0 });
goingLeft = true;
player.flipX('l');
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
player.move({ moveSpeed, 0 });
goingRight = true;
player.flipX('r');
}
//Event Loop:
while (window.pollEvent(Event)) {
switch (Event.type) {
case sf::Event::Closed:
window.close();
case sf::Event::KeyReleased:
if (Event.key.code == sf::Keyboard::Up) {
isJumping = false;
}
else if (Event.key.code == sf::Keyboard::Left) {
goingLeft = false;
}
else if (Event.key.code == sf::Keyboard::Right) {
goingRight = false;
}
}
}
if (player.getX() > window.getSize().x) {
position.x = player.getX();
}
else {
position.x = window.getSize().x;
}
//If player is in air and not jumping:
if (player.getY() < groundHeight && isJumping == false) {
player.move({ 0, moveSpeed });
}
followPlayer.setCenter(position);
window.clear();
window.setView(followPlayer);
window.draw(wall);
player.drawTo(window);
window.display();
}
}
this is my code. What I am trying to do is create a 2D platformer sidescroller. Everything works, except when the sprite goes past a certain point it'll just disappear. This also happens when I jump and move through the air at the same time. I cannot figure out why this is happening, any help would be greatly appreciated :)
I fixed the problem by just getting rid of my flipX function, and instead just creating a new sprite facing a different direction, then changing it everytime the user is facing a new direction
So I am learning C++ through game development and I recently ran across an error when I was trying to load an image called image1.png.
I tried using directories and that didn't work and then i moved the image to the same directory as the .cpp file and it still doesn't load the texture.
// defining libraries
#include <SFML/Graphics.hpp>
#include<iostream>
//global variables, functions, and classes
int main () {
//SETUPS
sf::RenderWindow window(sf::VideoMode(800, 600),"SFML Game");
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
bool play = true;
sf::Event event;
bool aPressed = false;
bool aReleased = false;
bool leftClick = false;
bool space = false;
bool returnReleased = false;
bool rightClick = false;
int numberOfClicks = 0;
int mouseX, mouseY;
int rectX = 0;
int rectY = 0;
sf::Texture image1;
if (image1.loadFromFile("image1.png") == -1) {
return 1;
}
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(100, 50));
rect.setPosition(0, 0);
rect.setTexture(&image1);
sf::CircleShape circle;
circle.setRadius(50);
circle.setPosition(50,50);
circle.setFillColor(sf::Color::Magenta);
//Game Loop
while (play == true) {
//EVENTS
while (window.pollEvent(event)) {
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::A) {
aPressed = true;
}
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::A) {
aReleased = true;
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) {
space = true;
}
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space) {
space = false;
}
if (event.type == sf::Event::Closed) {
play = false;
}
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape) {
play = false;
}
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
leftClick = true;
}
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Return) {
returnReleased = true;
}
if (event.type == sf::Event::MouseButtonPressed && event.key.code == sf::Mouse::Right){
rightClick = true;
}
}
//LOGIC
if (aPressed == true) {
std::cout << "Key has been pressed\n";
aPressed = false;
}
if (aReleased == true) {
std::cout << "Key has been released\n";
aReleased = false;
}
if (leftClick == true) {
numberOfClicks++;
std::cout << "Number of clicks: " << numberOfClicks << "\n";
leftClick = false;
}
if (event.type == sf::Event::MouseMoved) {
mouseX = event.mouseMove.x;
mouseY = event.mouseMove.y;
//std::cout << "X:" << mouseX << "\n";
//std::cout << "Y:" << mouseY << "\n";
}
if (returnReleased == true) {
std::cout << "Return Released \n";
returnReleased = false;
}
if (rightClick == true) {
numberOfClicks--;
std::cout << "Number of clicks: " << numberOfClicks << "\n";
rightClick = false;
}
rectX += 2;
rectY += 1;
rect.setPosition(rectX, rectY);
rect.setTexture(&image1);
//RENDERING
window.clear();
window.draw(rect);
window.draw(circle);
window.display();
}
//Clean Up
window.close();
return 0;
}
From SFML's page about image loading: http://www.sfml-dev.org/tutorials/2.0/graphics-sprite.php
The loadFromFile function sometimes fails with no obvious reason. First, check the error message printed by SFML in the standard output (check the console). If the message is „unable to open file”, make sure that the working directory (which is the directory any file path will be interpreted relatively to) is what you think it is: when you run the application from the explorer, the working directory is the executable folder, but when you launch your program from your IDE (Visual Studio, Code::Blocks, ...) the working directory is sometimes set to the project directory instead. This can generally be changed easily in the project settings.
Please check the error messages and post the error message.