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

Author Topic: changing texture on keypress  (Read 1181 times)

0 Members and 1 Guest are viewing this topic.

kingwill

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
changing texture on keypress
« on: July 08, 2013, 02:15:20 am »
hello i was experimenting trying to change the texture of a sprite on keypress but i realized that on keypress nothing happens what could i do to fix this problems



   sf::Texture tex[3];
   if(!tex[0].loadFromFile("data/Meifront.png",sf::IntRect(0,0,60.75f,76.0f))){}
   if(!tex[1].loadFromFile("data/Meifront.png",sf::IntRect(0,0,121.5f,76.0f))){}
   if(!tex[2].loadFromFile("data/Meifront.png",sf::IntRect(0,0,183.0f,76.0f))){}
   spr.setTexture(tex[0]);


void Game::GameLoop()
{

    while(window.isOpen())
    {
        while(window.pollEvent(e))
        {
            switch(e.type)
            {
                case sf::Event::Closed:
                    window.close();

                case sf::Event::KeyPressed :
                    if(e.key.code == sf::Keyboard::Escape)
                    {
                        window.close();
                    }
                    if(e.key.code == sf::Keyboard::Left)
                    {
                        tex[0].update(window, spr.getPosition().x, spr.getPosition().y);
                        spr.setTexture(tex[0]);
                        //Player1->move(-3,0);
                          }
                    if(e.key.code == sf::Keyboard::Right)
                    {
                        spr.setTexture(tex[1]);
                        Player1->move(3,0);
                    }
                    if(e.key.code == sf::Keyboard::Up)
                    {
                                                spr.setTexture(tex[2]);
                        }
                    if(e.key.code == sf::Keyboard::Down)
                    {
                        spr.setTexture(tex[3]);
                        }

                    }//switch
            }//while loop






 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: changing texture on keypress
« Reply #1 on: July 08, 2013, 03:29:12 am »
You need to go back and read the documentation/tutorials.

tex[0].update(window, spr.getPosition().x, spr.getPosition().y);

For starters calling update on a texture while passing a reference to a window causes the texture to copy from the window.

Player1->move(3,0);

Do not do movement inside your event loop, this is wrong. Also when you do movement you need to consider how much time has passed since your game will not run at the same speed on every computer.

if(!tex[0].loadFromFile("data/Meifront.png",sf::IntRect(0,0,60.75f,76.0f))){}
   if(!tex[1].loadFromFile("data/Meifront.png",sf::IntRect(0,0,121.5f,76.0f))){}
   if(!tex[2].loadFromFile("data/Meifront.png",sf::IntRect(0,0,183.0f,76.0f))){}

Without seeing your png file it looks also as if you do not understand the way sf::IntRect handles coordinates. sf::IntRect() takes the position and the size of an rectangle. The way you are doing it now you are going from a small rectangle to a larger rectangle while covering the same area as before.

Texture Documentation
Rect Documentation
« Last Edit: July 08, 2013, 03:31:22 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor