Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: View won't update even with setView() used  (Read 1849 times)

0 Members and 2 Guests are viewing this topic.

CyberWasp

  • Newbie
  • *
  • Posts: 4
    • View Profile
View won't update even with setView() used
« on: December 18, 2019, 12:23:13 am »
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!

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: View won't update even with setView() used
« Reply #1 on: December 18, 2019, 04:30:11 pm »
Hey.

What do you mean the view doesn't "update"?
You move your sprite, but do you move some of your views too?

CyberWasp

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: View won't update even with setView() used
« Reply #2 on: December 18, 2019, 05:15:11 pm »
Hey.

What do you mean the view doesn't "update"?
You move your sprite, but do you move some of your views too?

No. The views aren't programmed to move (just yet). And the player sprite doesn't move at all.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: View won't update even with setView() used
« Reply #3 on: December 18, 2019, 05:32:00 pm »
You're moving the sprite "player" in your Movement function, but do you draw it?
Or do you think you draw it when you draw the 3rd layer of view_obj but it's actually only a copy of it?

CyberWasp

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: View won't update even with setView() used
« Reply #4 on: December 18, 2019, 05:41:08 pm »
Oh... I feel so dumb. I totally overlooked that.

So what I should do is, from my understanding, update the sprite that is in view_obj, not player itself, isn't it so (Because the actual sprite that is being drawn is not player, it's the sprite in the view_obj array)? Then I'll need to do quite the revisions to my functions...

Thanks very much for bringing this to my attention, when I have the time to check this, I'll post further!

Again, thank you!
« Last Edit: December 18, 2019, 05:45:39 pm by CyberWasp »

CyberWasp

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: View won't update even with setView() used
« Reply #5 on: December 18, 2019, 08:35:54 pm »
You're moving the sprite "player" in your Movement function, but do you draw it?
Or do you think you draw it when you draw the 3rd layer of view_obj but it's actually only a copy of it?

Yep. That was the issue. Now the sprite is moving as it should. Thanks for taking your time to help me with this silly mistake.

Have a good day!
« Last Edit: December 18, 2019, 08:38:11 pm by CyberWasp »

 

anything