【问题标题】:Inherited sprite not draw-able继承的精灵不可绘制
【发布时间】:2018-01-27 16:21:45
【问题描述】:

在 SFML 中,我想要一个 sprite,但还有其他函数和变量,所以我决定创建一个继承 sprite 类的类,如下所示:
1.播放器.hpp

#pragma once

#include <stdio.h>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>

class Player : sf::Sprite{
    public:
        void setup();
        void input();
    private:
        int direction;
        void moveRight();
        void moveLeft();
        sf::Texture textures[8];
};

2(在制品)。播放器.cpp

void Player::setup(){
    direction = 0;
    textures[0].loadFromFile("images/player_right_still.png");
    textures[1].loadFromFile("images/player_right_jump.png");
    textures[2].loadFromFile("images/player_right_walk1.png");
    textures[3].loadFromFile("images/player_right_walk2.png");
    textures[4].loadFromFile("images/player_left_still.png");
    textures[5].loadFromFile("images/player_left_jump.png");
    textures[6].loadFromFile("images/player_left_walk1.png");
    setTexture(textures[0]);
}

void Player::input(){
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
        moveRight();
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
        moveLeft();
    }
}

void Player::moveRight(){

}

void Player::moveLeft(){

}
  1. game.cpp(图片是因为 SO 对我来说很奇怪)

但是当我编译它时,我得到了这个错误:

game.cpp: In function ‘int main()’:
game.cpp:23:27: error: ‘sf::Drawable’ is an inaccessible base of ‘Player’
         window.draw(player);

【问题讨论】:

    标签: c++ class inheritance sfml


    【解决方案1】:

    class Player : sf::Sprite 表示继承是私有的,即使用Player 类实例的代码将无法将其转换为Sprite 或访问从Sprite 类继承的方法。您应该将继承更改为public

    class Player: public sf::Sprite
    

    【讨论】:

    • 非常感谢!我已经坚持了好几天了!
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多