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

Author Topic: [RESOLVED]Changing Sprite Texture on key press not working?  (Read 3247 times)

0 Members and 1 Guest are viewing this topic.

JTeck

  • Newbie
  • *
  • Posts: 35
    • View Profile
Hey,

I've been stuck on this for a few hours now, and I'm lost. I have a if statement that lets me move my sprite, but for it to control the player, I need it to change the sprite texture according to what key is pressed,

I have this for my if statement to change the texture :

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)){ p_sprite.move(4,0); p_sprite.setTexture(tp_left);}

I have the textures setup in the header file as
sf::Texture tp_left;tp_left.loadFromFile('left.jpg');

Im drawing the sprite, the player loads fine and I can move correctly, just the changing of textures of the player on certain keys is confusing me.

Thanks :)
« Last Edit: June 13, 2014, 01:46:43 pm by JTeck »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Changing Sprite Texture on key press not working?
« Reply #1 on: June 13, 2014, 10:04:42 am »
I really hope, you don't actually code everything on one line, because that'd be some horrible code formatting!

Im drawing the sprite, the player loads fine and I can move correctly, just the changing of textures of the player on certain keys is confusing me.
Confusing you how? You haven't really told us what the problem is.
What do you expect to happen? What does actually happen?
Plus you should always provide a minimal and complete (directly compileable) code example. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

JTeck

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Changing Sprite Texture on key press not working?
« Reply #2 on: June 13, 2014, 10:21:10 am »
No, I don't do everything on the one line, the issue I have is that when I hit another key, it doesn't change the sprites texture.  This is the part of the script that controls the player and movement

#include <SFML/Graphics.hpp>
#include <iostream>
#include "Lester.h"


sf::Texture tp_sprite_l, tp_sprite_r, tp_sprite_d, tp_sprite;
sf::Sprite p_sprite, p_sprite_l, p_sprite_r, p_sprite_d;


void Lester::PlayerSetup()
{

    tp_sprite_l.loadFromFile("data/assets/player/left_char.png");
    tp_sprite_r.loadFromFile("data/assets/player/right_char.png");
    tp_sprite_d.loadFromFile("data/assets/player/down_char.png");

    tp_sprite.loadFromFile("data/assets/player/up_char.png");

    p_sprite.setTexture(tp_sprite);//Set Texture

    window->draw(p_sprite);
}
void Lester::KeyInput()
{

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)){ window->close(); }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))  p_sprite.move(-4, 0); m_view->move(-4, 0); std::cout << "LEFT" << std::endl;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)){ p_sprite.move(4, 0); m_view->move(4, 0); std::cout << "RIGHT" << std::endl; }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)){ p_sprite.move(0, 4); m_view->move(0, 4); std::cout << "DOWN" << std::endl; }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)){ p_sprite.move(0, -4); m_view->move(0, -4); std::cout << "UP" << std::endl; }
}
void Lester::Screen()
{

    window->setView(*m_view);//Set Game View
    Map();//Call Map
    window->draw(m_map);
    PlayerSetup();
}
void Lester::GameScreen()
{
    window->setView(*m_view);
    window->clear();
    Screen();
}
void Lester::GameLoop()
{
    window = new sf::RenderWindow(sf::VideoMode(960, 550, 32), "Lester V:0.1");
    m_view = new sf::View(sf::FloatRect(200, 200, 300, 200));//View Port
    m_view->zoom(0.7f);//Set Zoom

    window->setFramerateLimit(60);//Set FrameRate
    window->setVerticalSyncEnabled(true);

    p_sprite.setPosition(300, 250);

    while (window->isOpen())
    {

        sf::Event event;
        while (window->pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window->close();
        }
               
        running = true;

        while (splash_set == false){window->display();splash_set = true;}        
        if (splash_set == true){ window->clear(); KeyInput();PlayerSetup(); GameScreen(); window->display(); }
    }
}
void Lester::Init(){ GameLoop(); }

 

JTeck

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Changing Sprite Texture on key press not working?
« Reply #3 on: June 13, 2014, 01:45:21 pm »
Solved!

Had a line that was continuously replacing the texture back to the normal. Was in a function being looped and moved that out and works well.

Move this out of PlayerSetup and into Init()
p_sprite.setTexture(tp_sprite);//Set Texture