Hello everyone!
So, I was trying to make a simple platformer "game" where I have a character move left or right. I also decided I wanted to implement parallax scrolling, using views, so I can see how it's done. So, concerning the first one, I made the following simple code:
void main_control::Movement()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
player.setPosition(player.getPosition().x + 5, player.getPosition().y); //*player* is an sf::Sprite object
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
player.setPosition(player.getPosition().x - 5, player.getPosition().y);
}
}
Concerning the views system, I have created an array of 6 views (5 layers, 1 UI) and an array of 6 sf::Sprite vectors (in every vector, the sprites of each layer will be stored. Example, every sprite belonging to layer 3 will be stored in vector 3). The game will draw elements of each view like this:
#include "main_control.h"
#include "ViewHandler.h" //this class will be shown afterwards
void Stages::Draw(sf::RenderWindow & window)
{
window.setView(prob.view[0]);
prob.Draw(window, 0); //prob is an object from the ViewHandler class, a segment of which is shown below
player.Movement(); //player is an object from the main_control class
window.setView(prob.view[3]);
prob.Draw(window, 3);
}
(Note: for simplicity's sake, I removed the commands for the other 4 views, but the principle is the same. Also, I used this method shown, instead of a for loop, since I have been told this is more inexpensive.)The drawing of sprites is handled by another class, with the following function, Draw(), as you saw in the above segment:
void ViewHandler::Draw(sf::RenderWindow &window, int layer)
{
if (view_obj[layer].size() != 0)
{
for (int j = 0; j <= view_obj[layer].size() - 1; j++)
{
window.draw(view_obj[layer][j]);
}
} //the view_obj is the array of sf::Sprite vectors I was talking about
}
The problem is the following. When I try to move the player, the view in which [the player] is in (which in this example is view 3) will refuse to update. From what I have searched, this is usually due to not adding
setView() to the code. For me, it won't update even with that. I do get graphics displayed on screen, but I can't update them, they are stuck on what they were initialized.
The only thing I know is that this isn't propably due to how I have linked my classes together. I checked them, just to be sure (made the
Movement() function type something on the console) and they work properly. So the problem must narrow down to the graphics' side of things. What am I doing wrong and the views won't update?
I really hope you can help me with my problem!