lundi 20 juillet 2020

Inherited class appears to be resseting base variables

I am working on a game in c++ using SDL. I have made several classes, including a character class and a tile class. I made the character class inherit from the tile class. The tile class has several attributes such as an x and y. However, when I create an instance of the character class, every time I set the variables of the base class; the tile class, they always appear to be 0 when accessed later. But if I try access a variable belonging to the character class, it works as expected. I will try put all necessary code below, please let me know if you need anything else.

The tile.h file for clarity:

class Tile
{
    public:
        Tile(); 
        Tile(int x, int y, std::vector<int> animationSequence, int textureIndex, double animOffset, int width, int height);
    
        int getX();
        int getY();
        int getWidth();
        int getHeight();
        int getCurrFrame();
        int getTextureIndex();
        void setX(int x){this.x = x;}
        void setY(int y){this.y = y;}
        
        std::vector<TileType> getType();
        void setType(std::vector<TileType> type);
        double getAnimOffset(); 
        int getAnimSequenceSize();

        void render(int pos, Texture *myTexture);
    protected:
        int x, y;
        std::vector<int> animationSequence;
        int textureIndex;
        double animOffset;
        int width, height;
        int currFrame;
        std::vector<TileType> type;
};

The character.h file:

class Character: public Tile
{
    public:

        Character(std::vector<int> animationSequence, int textureIndex, double animationOffset, int width, int height, Texture* myTexture, int life = 100, int speed = 5);
        void render(int x, int y, double currCounter, Texture *myTexture);
        int getCharacterWidth();
        int getCharacterHeight();
        int getLife();
        void setLife(int life);
        int getSpeed();
        void setSpeed(int speed);
    protected:
        int life;
        int speed;
        int characterWidth;
        int characterHeight;
};

The specific method I'm having trouble with:

void Character::render(int x, int y, double currCounter, Texture *myTexture)
{
    //this->x at this point is always 0
    //however, this->characterWidth for example always has the correct value
    int pos = ((int)((double)(currCounter/animOffset))) % (int)animationSequence.size();
    setX(x);//I've also tried this->x = x, does exactly the same thing
    setY(y);
    //this->x at this point is the value of the x parameter
    Tile::render(pos, myTexture);
}

Aucun commentaire:

Enregistrer un commentaire