'player':未知覆盖说明符

问题描述

为什么我的代码不起作用???这个错误是什么意思?仅供参考:我想创建一个自上而下的射击游戏。我所有的代码基本上都是从不同的来源复制的(有些是我自己解决的)。 我打算将玩家类放在游戏循环或游戏类中。这是代码:

游戏.cpp

#include "Player.h"

//Private Functions
void game::intVariables()
{
    this->window = nullptr;

    this->points = 0;
    this->enemySpawnTimerMax = 1000.f;
    this->enemySpawnTimer = this->enemySpawnTimerMax;
    this->maxEnemies = 8;
}

void game::intWindow()
{
    this->videoMode.height = 1080;
    this->videoMode.width = 1780;
    this->window = new sf::RenderWindow(this->videoMode,"Top Down Shooter",sf::Style::Close | sf::Style::Titlebar);

    this->window->setFramerateLimit(60);

    this->window->setKeyRepeatEnabled(true);

    Player player("player.png");
    
}

void game::intEnemies()
{
    this->enemy.setPosition(30.f,30.f);
    this->enemy.setSize(sf::Vector2f(50.f,50.f));
    this->enemy.setFillColor(sf::Color(0,128,128));
    this->enemy.setOutlineColor(sf::Color::Black);
    this->enemy.setOutlineThickness(-4.f);
}

//Constructors or Destructors
game::game()
{
    this->intVariables();
    this->intWindow();
    this->intEnemies();
}

//prevents memory leak
game::~game()
{
    delete this->window;
}


//Accessors
const bool game::running() const
{
    return this->window->isOpen();
}

//functions
void game::player1()
{
    player.drawPlayer(*this->window);
}
    
void game::spawnEnemy()
{
    /*
        @return void

        Spawns enemies and sets their color and positions.
        - select random position
        - select random color
        - add enemy to vector
    */

    this->enemy.setPosition(
        static_cast<float>(rand() % static_cast<int>(this->window->getSize().x - this->enemy.getSize().x)),static_cast<float>(rand() % static_cast<int>(this->window->getSize().y - this->enemy.getSize().y))
    );

    this->enemy.setFillColor(sf::Color(0,128));

    //spawns the enemy
    this->enemies.push_back(this->enemy);
}

void game::pollEvents()
{
    //event polling
    while (this->window->pollEvent(this->evnt))
    {
        switch (this->evnt.type)
        {
        case sf::Event::Closed:
            this->window->close();
            break;
        case sf::Event::KeyPressed:
            if (this->evnt.key.code == sf::Keyboard::Escape)
                this->window->close();
            break;
        }
    }
}

void game::updateEnemies()
{
    /*
        @return void

        updates the enemy spawn timer and spawns enemies
        when the total amount of enemies is smaller than the maximum.
        removes the enemies at the edge of the screen. //to do
    */

    //Updating the timer for enemy spawning
    if (this->enemies.size() < this->maxEnemies)
    {
        if (this->enemySpawnTimer >= this->enemySpawnTimerMax)
        {
            //spawn the enemy and reset the timer
            this->spawnEnemy();
            this->enemySpawnTimer = 0.f; 
        }
        else
            this->enemySpawnTimer += 1.f;
    }
    // Move the enemies
        for (auto &e : this->enemies)
        {
            e.move(0.f,1.f);
        }
}



void game::update()
{
    this->pollEvents();

    this->updateMousePositions();

    this->updateEnemies();
}

void game::renderEnemies()
{
    for (auto &e : this->enemies)
    {
        this->window->draw(e);
    }
}


void game::render()
{   
    /* 
     - clears old frame
     - renders objects
     - display frames on window

      renders game objects.
    */

    this->window->clear(sf::Color(128,128));

    //draw game objects
    this->renderEnemies();

    this->player1();

    this->window->display();
}

void game::updateMousePositions()
{
    /*
    @return void

    updates the mouse position
    *mouse postion relative to window (vector2i)*
    */
    this->mousePosWindow = sf::Mouse::getPosition(*this->window);
}

game.h


#include <iostream>
#include <vector>
#include <ctime>

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>


/* class acts as the game engine
    wrapper class.
*/

class game
{
protected:
    //variables
    //window
    sf::RenderWindow* window;
    sf::VideoMode videoMode;
    sf::Event evnt;
    
    //mouse positions
    sf::Vector2i mousePosWindow;

    //game logic
    int points;
    float enemySpawnTimer;
    float enemySpawnTimerMax;
    int  maxEnemies;

    //game objects
    std::vector<sf::RectangleShape> enemies;
    sf::RectangleShape enemy;
    Player player;

    // Private Functions
    void intVariables();
    void intWindow();
    void intEnemies();
public:
    // Constructors or Destructors
    game();
    virtual ~game();

    // Accessors
    const bool running() const;
    
    // Functions
    void player1();
    void spawnEnemy();
    void pollEvents();
    void updateEnemies();
    void update();

    void renderEnemies();
    void render();
    void updateMousePositions();


};

播放器.h


#include <iostream>
#include <SFML/Graphics.hpp>
#include "game.h"

class Player
{
public:
    Player() {
        //default
    }
    Player(std::string imgDirectory) {
        if (!playerTexture.loadFromFile(imgDirectory)) {
            std::cerr << "Error\n";
        }
        playerSprite.setTexture(playerTexture);
    }
    void drawPlayer(sf::RenderWindow& window) {
        window.draw(playerSprite);
    }
    void movePlayer(char direction,float moveSpeed) {
        if (direction == 'u')
        {
            playerSprite.move(0,-moveSpeed);
        }
        else if (direction == 'd')
        {
            playerSprite.move(0,-moveSpeed);
        }
        else if (direction == 'l')
        {
            playerSprite.move(-moveSpeed,0);
        }
        else if (direction == 'r')
        {
            playerSprite.move(moveSpeed,0);
        }
    }
private:
    sf::Texture playerTexture;
    sf::Sprite playerSprite;
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...