10

我正在用 SDL 编写一个简单的游戏。我为我在游戏中使用的任何精灵构建了一个类层次结构。基类是 Sprite,它包含碰撞盒和 spritesheet 的数据抽象。紧接着是两个抽象类,Character 和 MapObject。

我目前正在实现派生自 Character 的 Player 类(Enemy 和 NPC 也将派生自抽象 Character 类)。

无论如何,希望这是有道理的。我的问题是这样的:

当我尝试在引擎中使用播放器类时,我无法访问任何 Sprite 函数。

我收到以下错误:

'Sprite' is not an accessible base of 'Player'

以下是头文件:

雪碧.h:

class Sprite{
public:
    virtual ~Sprite() = 0;  //I want this to be an abstract class and yes this is defined in the cpp

    //Initialization functions - MUST be called before anything else in the class can be used.
    void setupCollider(int xo, int yo, int ho, int wo);
    void setupSheet(SDL_Surface* dest, std::string file, int numClips, int ac, int _res, int sr);

    //SpriteSheet functions
    void setActiveClip(int a);
    int getActiveClip() const;
    void draw() const;
    int getResolution() const;
    SDL_Surface* getDest();


    //Collider functions
    void updateCollider();
    SDL_Rect box() const;
    bool collision(SDL_Rect other) const;

    //Access and Modify coordinates of the sprite
    void setLoc(int x, int y) { _x = x; _y = y; }
    int getX() const { return _x; }
    int getY() const { return _y; }

private:
    struct Collider{
        SDL_Rect  _box;
        int       x_offset,
                  y_offset;
    };

    struct SpriteSheet{
        SDL_Surface*     destination;
        SDL_Surface*    sheet;
        SDL_Rect*       clips;
        int             _numClips;
        int             active;
        int             res;
        int             sheetrows;
    };


    Collider     collisionAttributes;
    SpriteSheet  spriteSheetAttributes;
    int          _x, _y;

};

字符.h:

class Character : public Sprite{
public:
    virtual void move(Direction direction_input, const TileMap& currentlevel) = 0;
    virtual void animate() = 0;

    virtual void setAttributes( int h, int sp, int ad, int d, int m, int md, int l, std::string n){
        hearts = h; speed = sp; attackdamage = ad;
        defense = d; magicdamage = m; magicdefense = md;
        level = l; name = n;
    }

    bool isDead() const{
        return hearts == 0;
    }

    void heal(int heartsup){
        hearts += heartsup;
    }

    void dealDamage(int heartsDown){
        hearts -= heartsDown;
    }


protected:

    int hearts;
    int speed;
    int attackdamage;
    int defense;
    int magicdamage;
    int magicdefense;
    int level;
    std::string name;

};

播放器.h:

//Not fully finished yet, but should work for the purposes of this question
class Player : protected Character{
public:
    ~Player(){
        if(heart) SDL_FreeSurface(heart);
    }
    static const int HEART_WIDTH;

    void move(Direction direction_input, const TileMap& currentlevel);
    void animate();

    void updateCamera(TileMap& currLevel);


 private:

    SDL_Surface* heart;

    enum ClipFace
    {
        UP1,
        UP2,
        DOWN1,
        DOWN2,
        LEFT1,
        LEFT2,
        RIGHT1,
        RIGHT2
    };

    static const std::string HEART;
    static const int HEART_RES;

};

当我尝试从播放器中的 Sprite 调用设置函数时,我的引擎中出现第一个错误,第一个错误是:

player.setLoc(levels[0].startX(), levels[0].startY());

任何和所有的帮助表示赞赏。

[已解决] 编辑:评论的另一种解决方案:角色类没有从 Sprite 类继承任何东西,所以它在技术上不必从它派生。我没有让 Character 从 Sprite 继承,而是让 Player 从 Sprite 和 Character 继承,这也有效。我不确定什么是更好的设计。

4

3 回答 3

14

我认为你需要改变

class Player : protected Character{

class Player : public Character{

这样,您可以从程序中任何位置创建的实例访问对象上的CharacterSprite函数。PlayerPlayer

如果 aPlayer应该能够做任何 aCharacter可以做的事情,那么public继承是有意义的。没有理由隐藏可以在或对象Player中自由访问的东西。CharacterSprite

于 2012-08-12T01:28:24.470 回答
1

始终使用public继承来表示“is-a”关系。听起来像是Playera Character,所以说继承应该是public,不是protected

protected继承和private继承更像是“has-a”(在大多数情况下,成员子对象是处理这种关系的更简单方法)。

于 2012-08-12T01:28:46.857 回答
0

我敢打赌,既然Character是受保护的基类,它会限制对Sprite.

于 2012-08-12T01:27:51.430 回答