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

Author Topic: isKeyPressed() function not working  (Read 3643 times)

0 Members and 2 Guests are viewing this topic.

lockandstrike

  • Newbie
  • *
  • Posts: 32
    • View Profile
isKeyPressed() function not working
« on: November 12, 2013, 11:19:43 am »
So I'm building a game and it requires keyboard input. For some reason god know why the events didn't work so i moved on to the isKeyPressed function and it does not work at all. The code is bellow. Can some one please tell me what is wrong with my code?


HexGrid.hpp

#ifndef HEXGRID_HPP_
#define HEXGRID_HPP_

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

class HexGrid {
public:

        HexGrid(int height, int width, float radius, sf::Color color);
        void draw(sf::RenderWindow &App);

private:

        sf::CircleShape hexagon;
        sf::Color defColor;
        int defHeight;
        int hexNumber;
        int defWidth;
        int yRow;
        float defRadius;
        float xPos;
        float yPos;
    float yKeyPos;
    float xKeyPos;

    sf::Vector2f keyPos;
        sf::FloatRect hexBounds;

};

#endif

HexGrid.cpp

#include "HexGrid.hpp"

HexGrid::HexGrid(int height, int width, float radius, sf::Color color){

        defHeight = height;
        defWidth = width;
        defRadius = radius;
        defColor = color;
        hexNumber = width * height;
        yRow = 1;

}

void HexGrid::draw(sf::RenderWindow &App){

        hexagon.setRadius(defRadius);
        hexagon.setPointCount(6);
        hexagon.setFillColor(defColor);
        //sf::Vector2f poshex = hexagon.getPoint(2);



        xPos = hexagon.getRadius();
        yPos = hexagon.getRadius();
        // xKeyPos = hexagon.getRadius();
        // yKeyPos = hexagon.getRadius();

        for(int i = 1; i <= hexNumber; ++i){
               
                hexagon.setPosition(xPos, yPos);


        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) == true)
            yKeyPos = yKeyPos - ( hexagon.getRadius() * 2 );
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) == true)
            yKeyPos = yKeyPos + ( hexagon.getRadius() * 2);
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == true)
            xKeyPos = xKeyPos + ( hexagon.getRadius() * 2);
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == true)
            xKeyPos = xKeyPos - ( hexagon.getRadius() * 2);
               
                hexBounds = hexagon.getGlobalBounds();
        keyPos = sf::Vector2f(xKeyPos,yKeyPos);


                if( i % defWidth == 0 && i != 0 ){
                        if (yRow % 2 == 0){
                                xPos = hexagon.getRadius();
                                yPos += hexagon.getRadius() * 1.6;
                        }
                        else{
                xPos = hexagon.getRadius() * 1.822;
                                yPos += hexagon.getRadius() * 1.6;
                        }

                        ++yRow;
                }

                else
                        xPos += hexagon.getRadius() * 1.82;

        if( hexBounds.contains(keyPos) ){
                        hexagon.setOutlineThickness(5);
                        hexagon.setOutlineColor(sf::Color(50,50,50));
                        App.draw(hexagon);
                }
                else{
                        hexagon.setOutlineThickness(0);
                        App.draw(hexagon);
                }

        }

}
 

main.cpp

#include "HexGrid.hpp"

int main(){
        sf::RenderWindow app(sf::VideoMode(800,600,32),"Tyrion: TCoW");

        HexGrid hex(10,10,30.f,sf::Color(250,0,0));

        while(app.isOpen()){
                sf::Event evt;
                while(app.pollEvent(evt)){
                        if(evt.type == sf::Event::Closed)
                                app.close();
                }
                app.clear(sf::Color(250,250,250));

                hex.draw(app);

                //HexGrid(5, 5, 30, sf::Color(50,50,50),app);


                app.display();
        }
        return 0;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: isKeyPressed() function not working
« Reply #1 on: November 12, 2013, 11:28:28 am »
You should test things in a minimal application (it shouldn't take more than 10 lines of code). Then if it still doesn't work, you can post the new code as well as more details (version of SFML, OS, compiler, ..).

SFML events and inputs most likely work, it's rather the surrounding code that has errors. After a quick look, I can already spot many mistakes and undefined behaviours: your movement doesn't depend on the elapsed time, you use uninitialized variables, etc. That's why testing in a minimal code is often useful: it can help you realize that it's your code that doesn't work, which is totally independant from SFML functions.
Laurent Gomila - SFML developer

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: isKeyPressed() function not working
« Reply #2 on: November 15, 2013, 03:33:57 am »
You also have your event handling in your draw method which is all kinds of wrong.