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

Author Topic: Sprite TextureRect Rect<int> not changing, animations are not working  (Read 870 times)

0 Members and 1 Guest are viewing this topic.

Temporalus

  • Newbie
  • *
  • Posts: 2
    • View Profile
General info
OS - Windows 8
graphics card - Intel(R) HD Graphics 4000 (on a laptop)
SFML v - 2.0
compiler - VSC++ 2012 express for windows 8 desktop


When the player moves in the downward direction, the animation plays, very quickly, but I know how to fix that. The problem I'm stuck on is that when the player moves up, left, or right, the animations don't play and the player jut looks like he's gliding across the ground.

here is the source - I don't think I left anything relevant out, but sorry if I did, just lemme know.

bool Engine::Init(){
        groundTextures = new TextureManager(0, 1, allTextures);
        entityTextures= new TextureManager(2, 3, allTextures);
        currentLevel = new Level(videoSize.x, videoSize.y);
        camera = new Camera(videoSize.x, videoSize.y, 0.7);
        window = new sf::RenderWindow(sf::VideoMode(800,600, 32), title);
        player = new Entity(12, 12, entityTextures, 1);
        //sf::RenderWindow* window;
        if(!window)
                return false;
        //LoadTextures("sprite1.png");
        currentLevel->LoadLevel(groundTextures,tileSize);
        return true;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Engine::ProcessInput(){
        sf::Event evt;
        //Loop through all window events
        while(window->pollEvent(evt)){
                if(evt.type == sf::Event::Closed){
                        window->close();
                }
                if(sf::Mouse::isButtonPressed(sf::Mouse::Right)){
                        sf::Vector2i localPosition = sf::Mouse::getPosition(*window);
                        RMouseDown = true;
                }
                if(evt.type == sf::Event::MouseButtonReleased){
                        RMouseDown = false;
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
                        player->moving = true;
                        player->setFacing("leftFacing");
                        // left key is pressed: move our character
                        player->moveEntity();
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
                        player->moving = true;
                        player->setFacing("rightFacing");
                        // left key is pressed: move our character
                        player->moveEntity();
                }      
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
                        player->moving = true;
                        player->setFacing("upFacing");
                        // left key is pressed: move our character
                        player->moveEntity();
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
                        player->moving = true;
                        player->setFacing("downFacing");
                        // left key is pressed: move our character
                        player->moveEntity();
                }
                else
                        player->moving = false;
        }

}

////////////////////////////////////////////////////////////////////////////////////////////////

void Engine::RenderFrame(){    
        //Camera offsets


        window->clear();
       
        camera->camRender(currentLevel, tile, tileSize, window);
        player->entDraw(window);

        window->display();
}

////////////////////////////////////////////////////////////////////////////////



Entity::Entity(int x, int y, TextureManager* entityTextures, int texLoc){
        upFacing = new sf::Rect<int>(32, 0, 32, 32);
        rightFacing = new sf::Rect<int>(32, 32, 32, 32);
        downFacing = new sf::Rect<int>(32, 64, 32, 32);
        leftFacing = new sf::Rect<int>(32, 96, 32, 32);
       
        this->entitySprite.setTexture(*entityTextures->at(texLoc));
        this->entitySprite.setPosition(x*32, y*32);
        this->direction = leftFacing;
        moving = false;
        attacking = false;

}

void Entity::setFacing(string facing){
        if(facing == "leftFacing")
                this->direction = leftFacing;
        if(facing == "rightFacing")
                this->direction = rightFacing;
        if(facing == "upFacing")
                this->direction = upFacing;
        if(facing == "downFacing")
                this->direction = downFacing;
}
void Entity::entDraw(sf::RenderWindow* rw){
        if(moving == true){
                        //if(direction == leftFacing){
                                if(direction->left >= 32)
                                        direction->left = 0;
                                else
                                        direction->left = 64;
                        //}
        }
        else
                direction->left = 32;
        entitySprite.setTextureRect(*direction);
        rw->draw(entitySprite);
}

 


I really appreciate any help whatsoever, if you give me any advice, even if its not a solution, it helps my understanding and brings me closer to a solution, so don't hesitate to say anything :D

Temporalus

  • Newbie
  • *
  • Posts: 2
    • View Profile
In fact I actually did forget something, but it wasn't relevant code. This is the .png file that the textureRect draws from. I dont think you should need the TextureManager class, all it does is maintain a list of pointers to the texture files, and the one called in the entity is the attached file

[attachment deleted by admin]

 

anything