After I fixed my linker problems, I stumbled upon another error.
I wrote a code for a small game and I created like a request if-loop to read out the keyboard inputs (Up, Down, Left, Right), and now, to clean up my code I want to put the loop in a seperate function, or even a seperate header file/class.
But I am not able to succeed
First of all: here is my code:
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "MTGame");
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile("./data/test.jpg"))
return EXIT_FAILURE;
sf::Sprite sprite(texture);
// Load a music to play
sf::Music music;
if (!music.openFromFile("./data/background.wav"))
return EXIT_FAILURE;
// Play the music
music.play();
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window -> exit
if (event.type == sf::Event::Closed)
window.close();
}
//Move-Loop
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
sprite.move(-1, 0);
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
sprite.move(1, 0);
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
sprite.move(0, 1);
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
sprite.move(0, -1);
}
//End-Loop
// Clear screen
window.clear();
// Draw the sprite
window.draw(sprite);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
The section //Move-Loop is the part I want to put in a seperate function.
So basically I just have to declare a function with a sf::Sprite as parameter which then represents the sprite that is supposed to get moved...
. . .
void mover(sf::Sprite temp) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
temp.move(-1, 0);
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
temp.move(1, 0);
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
temp.move(0, 1);
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
temp.move(0, -1);
}
But that does not work...
I am kinda new to C++ (as you can see
) but was pretty stable with coding until this point.
So could anyone please help me.
Best with a working solution with outsourced classes (in a header file).
Thank you for your help in advance...
(I don't know if this is the right place to post this stuff. If not please tell me where to put it and I will copy it there and delete this post)