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

Author Topic: Mouse/Keyboard Event issue  (Read 4078 times)

0 Members and 1 Guest are viewing this topic.

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Mouse/Keyboard Event issue
« on: November 26, 2012, 06:32:08 pm »
Alright... I have done everything in my power to fix this but it turns out.... I can't fix it.
Basically I call a check to see if a button on the keyboard is pressed and which button is being pressed.
Then it does what it is supposed to do(Move player left or right), And it works... BUT!

When ever I move the mouse, it does the same thing(Move the player to the right).
Does the problem rely on the mouse I am using(Razer - Black Mamba) or am I an idiot?
Here is a snippet of my code:

        if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::D))
        {
            //Move to the Right and shift one frame on the animation tile
            Player::FrameSize.left = 32 * Player::frame;
            Player::FrameSize.top = 0;
            Player::VELx += 0.01;
        }

        if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::A))
        {
            //Move to the Left and shift one frame on the animation tile
            Player::FrameSize.left = 32 * Player::frame;
            Player::FrameSize.top = 32;
            Player::VELx += -0.01;
        }
        if((event.type == sf::Event::KeyReleased) && (event.key.code != sf::Keyboard::D))
        {
            //if button is not being pushed... Reset the animation
            Player::FrameSize.left = 0;
            Player::VELx = 0;
            Player::clock.restart();
        }

        if((event.type == sf::Event::KeyReleased) && (event.key.code != sf::Keyboard::A))
        {
            //if button is not being pushed... Reset the animation
            Player::FrameSize.left = 0;
            Player::VELx = 0;
            Player::clock.restart();
        }

Oh and also... Note that the VEL stuff is not 100% thought through yet, I needed to test the movement of the sprite and its animation.
As for the "(event.type == sf::Event::KeyReleased)" and "(event.type == sf::Event::KeyPressed)", has no effect if it was like:

        if(event.type == sf::Event::KeyPressed){
        if(event.key.code == sf::Keyboard::D)
        {
            Player::FrameSize.left = 32 * Player::frame;
            Player::FrameSize.top = 0;
            Player::VELx += 0.01;
        }

        if(event.key.code == sf::Keyboard::A)
        {
            Player::FrameSize.left = 32 * Player::frame;
            Player::FrameSize.top = 32;
            Player::VELx += -0.01;
        }
        }

Any insight or fix or anything would be most greatly appreciated.
If you notice I put "....", in my sentences way too much... I'm sorry.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Mouse/Keyboard Event issue
« Reply #1 on: November 26, 2012, 07:12:59 pm »
So you're saying that the following code constantly prints out 'W pressed' whenever you move the mouse over the window?

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1024, 765), "Keyboad Events");
    window.setFramerateLimit(60);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
            else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::W)
                std::cout << "W pressed" << std::endl;
        }

        window.clear();
        window.display();
    }
}
 

Btw you should always post a complete example. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Mouse/Keyboard Event issue
« Reply #2 on: November 26, 2012, 07:40:09 pm »
Yes, but its not W but D... But I think you get the point.
I would also like to show the full source code but its kinda funky looking.
Note I did mess with it a little... trying to fix it.

But to get it fixed... Ya sure:

#include "Player.h"

void Player::Update(sf::Event event)
{
    Player::setTextureRect(Player::FrameSize);

        if((event.type == sf::Event::KeyReleased) && (event.key.code != sf::Keyboard::D))
        {
            //if button is not being pushed... Reset
            Player::FrameSize.left = 0;
            Player::VELx = 0;
            Player::clock.restart();
        }

        if((event.type == sf::Event::KeyReleased) && (event.key.code != sf::Keyboard::A))
        {
            //if button is not being pushed... Reset
            Player::FrameSize.left = 0;
            Player::VELx = 0;
            Player::clock.restart();
        }



    if(Player::frame == 6)
    {
        //if over frame limit for running... reset
        Player::frame = 1;
    }

    if(FrameSize.left == 192)
    {
        //if over frame limit for running... reset
        Player::FrameSize.left = 32;
    }
        if(event.type == sf::Event::KeyPressed){
        if(event.key.code == sf::Keyboard::D)
        {
            Player::FrameSize.left = 32 * Player::frame;
            Player::FrameSize.top = 0;
            Player::VELx += 0.01;
        }

        if(event.key.code == sf::Keyboard::A)
        {
            Player::FrameSize.left = 32 * Player::frame;
            Player::FrameSize.top = 32;
            Player::VELx += -0.01;
        }
        }

        if((event.type == sf::Event::KeyReleased) && (event.key.code == sf::Keyboard::D))
        {
            Player::VELx -= 0.01;
        }

        if((event.type == sf::Event::KeyReleased) && (event.key.code == sf::Keyboard::A))
        {
            Player::VELx -= -0.01;
        }


    Player::Location.x += VELx * Player::Seconds();
    Player::move(Player::Location);
    Player::frame++;
}

Player::Player()
{
    Player::frame = 1;
    if (!Player::ActorImage.loadFromFile("Player.png"))
    {
        std::string FAIL = "Failed to load Player Texture!";
        WriteLogLine(FAIL);
    }

    VELx = 0;
    VELy = 0;
    Player::setTexture(Player::ActorImage);
    FrameSize.height = 32;
    FrameSize.width = 32;
    Player::setTextureRect(Player::FrameSize);
    sf::Vector2f Scale;
    Scale.x = 2;
    Scale.y = 2;
    Player::setScale(Scale);
    Player::sethealth(100);
    Player::setPassive(false);
}
If you notice I put "....", in my sentences way too much... I'm sorry.

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Mouse/Keyboard Event issue
« Reply #3 on: November 26, 2012, 07:47:28 pm »
Hold on.... Whoa... I had accidentally set this:
       
if((event.type == sf::Event::KeyReleased) && (event.key.code == sf::Keyboard::D))
        {
            Player::VELx -= 0.01;
        }

        if((event.type == sf::Event::KeyReleased) && (event.key.code == sf::Keyboard::A))
        {
            Player::VELx -= -0.01;
        }
 
Well, that's embarrassing... Removed it.... still nothing.
If you notice I put "....", in my sentences way too much... I'm sorry.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Mouse/Keyboard Event issue
« Reply #4 on: November 26, 2012, 07:50:07 pm »
Yes, but its not W but D... But I think you get the point.
So you compiled and ran my code?
If you did so and got the output when you move your mouse, then there's something strange going on with your mouse or it's something in a code piece you didn't show us.

I would also like to show the full source code but its kinda funky looking.
Okay my bad, don't show the full source, but a minimal and complete example, like the one I showed above. But as I said if the example above produced the error, then there's something wrong with your system/mouse. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Mouse/Keyboard Event issue
« Reply #5 on: November 26, 2012, 07:56:51 pm »
Quote
Okay my bad, don't show the full source, but a minimal and complete example, like the one I showed above. But as I said if the example above produced the error, then there's something wrong with your system/mouse.

Thats what I'm thinking... I will redownload the SFML 2.0 snapshot and recompile it.
I'm also starting to think its my mouse too.... Going to test another mouse in a second while thats compile and post back.
If you notice I put "....", in my sentences way too much... I'm sorry.

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Mouse/Keyboard Event issue
« Reply #6 on: November 26, 2012, 08:04:28 pm »
Tried another mouse... No dice... And the Recompile too.... Nothing.
If you notice I put "....", in my sentences way too much... I'm sorry.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Mouse/Keyboard Event issue
« Reply #7 on: November 26, 2012, 08:06:21 pm »
Tried another mouse... No dice... And the Recompile too.... Nothing.
Are you referring to your or my code, because you still didn't confirm/deny that the problem exists with my code?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Mouse/Keyboard Event issue
« Reply #8 on: November 26, 2012, 08:16:07 pm »
Forgot sorry... No your code worked, which is odd.
Er, I looked through everything and my code meet everything except maybe the way you did the if statement.
Tried the "Else if" thing on yours... nothing.
The pollevent function is called outside of the Player::Update command and it would break my code if it was in the Player::Update function.


Should I post ALL of my code?
If you notice I put "....", in my sentences way too much... I'm sorry.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Mouse/Keyboard Event issue
« Reply #9 on: November 26, 2012, 08:19:54 pm »
Should I post ALL of my code?
No you should reduce your code to the absolute minimum.
Remove every piece that's not essential and the problem still occurs, in doing so you might find the problem on your own.

And if you're really so lazy pack the source and upload it somewhere (e.g. https://legacy.sfmluploads.org/)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Mouse/Keyboard Event issue
« Reply #10 on: November 26, 2012, 08:24:47 pm »
Almost couldn't read the text :P
https://legacy.sfmluploads.org/file/157
I use codeblocks to build and code but I have a prebuilt exe in there, ready to run.

Edit: I may have removed the "Drawing.hpp" file, because it was empty.
« Last Edit: November 26, 2012, 08:26:26 pm by Halsys »
If you notice I put "....", in my sentences way too much... I'm sorry.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Mouse/Keyboard Event issue
« Reply #11 on: November 26, 2012, 08:38:06 pm »
Almost couldn't read the text :P
Darn, next time I'll use 1pt...

Besides the codebase being quite, well ehrm ugly, the problem is I think, that you use the sf::Event as member of the class, thus it somehow still holds the old event when calling Player.Update(event) and so it gets recognized as key press. I'd say this shouldn't happen, but it's the only thing that makes kind of sense. Try putting the sf::Event event right underneath while(Game::isOpen()) {.

As a general advice, it's usually not common to call member functions with Classname::function(), but simply with function(). Also having a file that includes every header (IncludeFile.h) is a very bad idea. And a detail, since your programming in C++ it makes more sense (to me) to use .hpp ending, so whenever you see C++ code, you get .cpp/.hpp and for C code .c/.h. Unfortunately not too many programmer follow this.
There are many other things that could be nicer, so I suggest you take a look at some sources of other projects to get an idea of a bit a cleaner codebase. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Mouse/Keyboard Event issue
« Reply #12 on: November 26, 2012, 08:55:42 pm »
Quote
Besides the codebase being quite, well ehrm ugly

I'm completely aware that it looks Very ugly... I don't even like it. :P
I'm also thinking about starting over and trying again if I cant fix this.

Quote
Try putting the sf::Event event right underneath while(Game::isOpen()).

Did it, nothing...

Quote
As a general advice, it's usually not common to call member functions with Classname::function(), but simply with function(). Also having a file that includes every header (IncludeFile.h) is a very bad idea. And a detail, since your programming in C++ it makes more sense (to me) to use .hpp ending, so whenever you see C++ code, you get .cpp/.hpp and for C code .c/.h. Unfortunately not too many programmer follow this.
There are many other things that could be nicer, so I suggest you take a look at some sources of other projects to get an idea of a bit a cleaner codebase.

Just a function?Not in a class? Well I kinda  wanted it to be a exclusive thing with player because I will soon have more stuff added like enemies and they need to update too.

I saw someone do the includefile.h thing and it seemed legit to me because he would only have to include it once, right?

On my other projects I had hpp...  but I just let Codeblocks do it this time for me and didn't really care to much about it.
Mostly because it ran anyway and it didn't bother me that much... to each, there own.

Looking at other projects? Noted...
If you notice I put "....", in my sentences way too much... I'm sorry.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Mouse/Keyboard Event issue
« Reply #13 on: November 26, 2012, 09:18:40 pm »
I saw someone do the includefile.h thing and it seemed legit to me because he would only have to include it once, right?

On my other projects I had hpp...  but I just let Codeblocks do it this time for me and didn't really care to much about it.
Mostly because it ran anyway and it didn't bother me that much... to each, there own.
Well it's not about what's most comfortable or nice, but it has an immense effect on the compile time and you won't run into any inclusion problem if you only include what's needed.
Maybe you should read again a bit on that topic and about 'forward declarations'. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Mouse/Keyboard Event issue
« Reply #14 on: November 26, 2012, 09:21:15 pm »
Might just rethink how the game is going to be built... as you saw... Everything was Everywhere.
It needs to be simple and clean.... Back to the drawing board and taking you up on that advice.
If you notice I put "....", in my sentences way too much... I'm sorry.