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

Author Topic: [SOLVED] Movement Issues  (Read 1435 times)

0 Members and 1 Guest are viewing this topic.

EpiQuerty

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] Movement Issues
« on: April 19, 2016, 01:54:50 am »
Hi,
I'm new to SFML, and so far things have gone pretty smoothly. However, when I try to make the player move, nothing happens. As in, the circle gets printed to the screen, but it doesn't move when I try to use key inputs. I tried putting it all into one file in case it was a multi-file issue, but to no avail. To my knowledge, I should be doing everything right, yet the image refuses to move.

Here's all my (relevant) code:

#pragma once
#include "declare.h"//this is a file that just includes the SFML files and adds some namespaces
//(that's why you don't see the "sf::" before anything, I set it as a namespace)

CircleShape player; //just a placeholder for the player, I don't have all the textures done for the game yet

void DrawPlayer()
{
        player.setRadius(100);
        player.setFillColor(Color::Blue);
        player.setOutlineThickness(5);
        player.setPosition(0, 0);
        window.draw(player);
        window.display();
}

void PlayerInput()
{
                if (Keyboard::isKeyPressed(Keyboard::W))
                {
                        player.move(0, 10);
                }
                else if (Keyboard::isKeyPressed(Keyboard::S))
                {
                        player.move(0, -10);
                }

                else if (Keyboard::isKeyPressed(Keyboard::A))
                {
                        player.move(-10, 0);
                }
       
                else if (Keyboard::isKeyPressed(Keyboard::D))
                {
                        player.move(Vector2f(0, 50));
                }
       
}

void Player()
{
        DrawPlayer();
       
        PlayerInput();
}
 

and here's the main function:

int main()
{
        init();
        GraphicsInit(); //initialization functions. They just load some placeholder textures and stuff like that

        window.create(VideoMode(1024, 768), "Game Engine");
        window.clear(Color::Black);
        window.display();

        DrawPlayer();

        while (window.isOpen()) //main window loop
        {
                while (window.pollEvent(event)) //secondary window loop, polls for closing events
                {
                        if (event.type == Event::Closed)
                        {
                                window.close();
                        }
                        PlayerInput();
                }
        }
}
 

I'm not sure if this is a SFML problem, a C++ problem, or just me not seeing something obvious (I haven't made anything as large as a game up until now, there's still some C++ stuff I don't know too well).

Keep in mind that this is some prototype code, so optimization isn't high on my priorities list right now. If you have some suggestions, though, that'd be nice  :D.

Anyway, back on track, does anyone see anything wrong with my code? I'd really like to fix this whole "no movement" problem... It's been bugging me for a few days now.
« Last Edit: April 19, 2016, 11:11:13 pm by EpiQuerty »

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: Movement Issues
« Reply #1 on: April 19, 2016, 05:31:35 am »
Hi EpiQuerty,

In your main function, you are drawing the player once and then you start up a loop where you check for player input and then move the position of the player. 

The problem is moving the position of the player needs to be followed by another draw() function after you call PlayerInput().  This should be a different than DrawPlayer() since that's just an initial draw.

Drawing needs to happen once per loop iteration (actually a clear screen followed by a draw of the entities at their new positions).

Also I would take your PlayerInput() call out of the inner while loop, and move into the outer loop

int main()
{
    init();
    GraphicsInit(); //initialization functions. They just load some placeholder textures and stuff like that

    window.create(VideoMode(1024, 768), "Game Engine");
    window.clear(Color::Black);
    window.display();

    DrawPlayer();

    while (window.isOpen()) //main window loop
    {
        while (window.pollEvent(event)) //secondary window loop, polls for closing events
        {
            if (event.type == Event::Closed)
            {
                window.close();
            }
        }
       
        PlayerInput();
        NewDrawFunction();
    }
}
 

 

I hope this helps!
« Last Edit: April 19, 2016, 05:40:28 am by erdrick22 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Movement Issues
« Reply #2 on: April 19, 2016, 05:10:03 pm »
I agree with Erdrick's solution.
Just to clarify, though:
"DrawPlayer" should be setting up the player sprite (maybe change its name?); window stuff (including drawing) is not required here.
"NewDrawFunction" should be the window stuff removed from the other function. Remember to make sure that it also include the clear though (clear, draw, display).
"PlayerInput" should not be in the inner (event) loop as it's not dealing with - or re-acting to - any events. It's merely testing the current, real-time state of keys (and moving the player).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

EpiQuerty

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Movement Issues
« Reply #3 on: April 19, 2016, 11:10:57 pm »
I took both of you guys' advice, and it worked! Thanks a lot, and I'll make sure to separate window stuff and graphics setup in the future :D.

Marking this post as solved.

 

anything