gpt4 book ai didi

c++ - 继承的 Sprite 不可绘制

转载 作者:行者123 更新时间:2023-11-28 01:44:21 34 4
gpt4 key购买 nike

在 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

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

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

最佳答案

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

class Player: public sf::Sprite

关于c++ - 继承的 Sprite 不可绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45765823/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com