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

Author Topic: How to move sprite with keyboard?  (Read 9476 times)

0 Members and 1 Guest are viewing this topic.

sakib_hasan63

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
How to move sprite with keyboard?
« on: October 10, 2016, 04:17:12 pm »
I have started working on a simple game project. But I can't move the sprite  as it should be.Sprite is not moving at the right direction. It moves right even if I press 'left arrow' key. Please can anyone find out the problem. :)
here is the code :


#include<SFML/Graphics.hpp>
#include<iostream>
int W=900,H=600;

int main(){
    sf::RenderWindow window(sf::VideoMode(W,H),"My window");
    sf::Texture t1,t2;
    t1.loadFromFile("sprite1.png");
    t2.loadFromFile("bground.jpg");
    sf::Sprite s1,background;
    s1.setTexture(t1);
    background.setTexture(t2);
    //sf::Vector2i source(50,400);
    int direction=1,shiftX=10;


    while(window.isOpen()){
        sf::Event event;
        while(window.pollEvent(event)){
           switch(event.type){
                case sf::Event::Closed:
                    window.close();
                    break;
                    break;
                case sf::Event::KeyPressed:
                    if(event.key.code= sf::Keyboard::Left){
                         shiftX+=50;
                    }
                    else if(event.key.code= sf::Keyboard::Right){
                        shiftX-=50;
                    }
                    else if(event.key.code==sf::Keyboard::Up){
                        direction++;
                    }
                    else if(event.key.code==sf::Keyboard::Down){
                        direction--;
                    }
                    break;
            }

        }
        if(shiftX<0){
            shiftX=500;
        }
        else if(shiftX>500){
            shiftX=0;
        }
        if(direction>2){
            direction =0;
        }
        else if(direction<0){
            direction =2;
        }
        s1.setPosition(shiftX,400);
        sf::IntRect pos(200*direction,0,190,192);
        window.clear();
        s1.setTextureRect(pos);
        window.draw(background);
        window.draw(s1);
        window.display();
    }
}

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: How to move sprite with keyboard?
« Reply #1 on: October 10, 2016, 07:34:49 pm »
In C++ the equality operator is ==

Merle

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: How to move sprite with keyboard?
« Reply #2 on: October 10, 2016, 11:05:08 pm »
Sprite.move(int x, int y);
 

this function moves the sprite in either direction, so you could say

using namespace sf;

int changeX = 0;
int changeY = 0;
 

and then when you poll for events in the game loop

if (isKeyPressed(Keyboard::A)) {
    changeX = -2;
} else if (isKeyPressed(Keyboard::D) {
    changeX = 2;
} else {
    changeX = 0;
} if (isKeyPressed(Keyboard::W) {
    changeY = -2;
} else if (isKeyPressed(Keyboard::S)) {
    changeY = 2;
} else {
    changeY = 0;
}
 

and when your updating

Sprite.move(changeX, changeY);
 

 

anything