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

Author Topic: Y Position rapidly going up. Invalid texture size.  (Read 1058 times)

0 Members and 1 Guest are viewing this topic.

PlatformDeveloper

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Y Position rapidly going up. Invalid texture size.
« on: June 29, 2017, 08:17:11 pm »
Hello guys, I need a little help with my code. I cant give you guys a working example because my code is spread out between around 500 files. But here is one of my classes I need help with.

class AnimatedTile
{
protected:
        std::vector<sf::RectangleShape> Frames = std::vector<sf::RectangleShape> (10);
        sf::Texture texs[11];
        int FrameNum = 0;
        int upZero = 0;
public:
        AnimatedTile()
        {

        }

        AnimatedTile(float s1, float s2, float x1, float y1, sf::String Spritesheet)
        {
                sf::Image img;
                if (!img.loadFromFile(Spritesheet))
                        return;
                sf::Texture texsA[11];
                for (int i = 0; i < 11; i++)
                {
                        texs[i] = texsA[i];
                        sf::RectangleShape pb;
                        if (!texs[i].loadFromImage(img, sf::IntRect(64 * i, 94 * i, 64, 94)))
                                return;
                        pb.setTexture(&texs[i]);
                        pb.setSize(sf::Vector2f(s1, s2));
                        pb.setPosition(x1, y1);
                        Frames.push_back(pb);
               
                }
        }

        sf::Vector2f getPosition()
        {
                return Frames.at(FrameNum).getPosition();
        }

        void setPosition(float x, float y)
        {
                for (int i = 0; i < 11; i++)
                        Frames[i].setPosition(x, y);
                return;
        }

        sf::Rect<float> getGlobalBounds()
        {
                return Frames[FrameNum].getGlobalBounds();
        }

        sf::Vector2f getOrigin()
        {
                return Frames[FrameNum].getOrigin();
        }

        void nextFrame()
        {
                if (FrameNum == 10 && upZero == 0) {
                        FrameNum--;
                        upZero = 1;
                }
                else if (FrameNum == 0 && upZero == 1) {
                        FrameNum++;
                        upZero = 0;
                }
                else if (upZero == 0)
                        FrameNum++;
                else if (upZero == 1)
                        FrameNum--;
        }

        virtual void draw(sf::RenderTarget& target, sf::RenderStates states = *new sf::RenderStates()) const
        {
                target.draw(Frames[FrameNum]);
        }
};

There is an invalid texture size error, and the Y position of my player is rapidly going up.
The program compiles correctly so I do not know where the error is.

Here is my spritesheet,
https://www.dropbox.com/s/9nn3u14hv8a0fx7/p1_walk.png?dl=0

Thanks,
PlatformerDeveloper.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Y Position rapidly going up. Invalid texture size.
« Reply #1 on: June 30, 2017, 08:50:21 am »
Use a debugger and figure out what is going on.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/