Я попытался сделать игру SFML C ++ 2D Space Shooter. Итак, я сделал игру, следуя руководству, в которой использовались основные формы для врагов. Вместо этого я пытался вставить в них текстуры и спрайты. Это сработало правильно, и Sprite появился на экране, но когда я попал в рандомизацию нереста врагов на экране, я попал в проблему того, как враги все еще не появлялись в левом углу экрана. Хотелось бы немного понять проблему. < /P>
my cpp -файл: < /p>
#include "Game.h"
using namespace sf;
//конструктор и деструктор
Game::Game()
{
this->initWindow();
this->initTextures();
this->initPlayer();
this->initEnemies();
}
Game::~Game()
{
delete this->window;
delete this->player;
for (auto &i : this->textures) {
delete i.second;
}
for (auto &i : this->enemies) {
delete i;
}
}
void Game::initEnemies() {
this->spawnTimerMax = 20.f;
this->spawnTimer = this->spawnTimerMax;
}
void Game::updateEnemies() {
this->spawnTimer += 0.5f;
if (this->spawnTimer >= spawnTimerMax) {
this->enemies.push_back(new Enemy(rand() % 1920, rand() % 1080));
this->spawnTimer = 0.f;
}
for (auto *enemy : this->enemies) {
enemy->update();
}
}
void Game::update() {
this->updatePollEvents();
this->updateInput();
this->player-\>update();
this->updateBullets();
this->updateEnemies();
}
void Game::render() {
this->window->clear());
this->player->render(*this->window);
for (auto &enemy : this->enemies) {
enemy->render(*this->window);
}
this->window->display();
}
**My header file Game.h:**
#include
#include
#include "Player.h"
#include "Bullet.h"
#include "Enemy.h"
using namespace sf;
class Game
{
private:
RenderWindow* window;
float spawnTimer;
float spawnTimerMax;
std::vector enemies;
};
> Edit: **so Enemy.cpp is:**
#include "Enemy.h"
using namespace sf;
void Enemy::initTexture() {
if (!this->texture.loadFromFile("textures/enemy1.png")) {
std::cout texture);
this->sprite.scale(1.1f, 1.1f);
}
void Enemy::initVariables() {
this->type = 0;
this->hpMax = 10;
this->hp = 0;
this->damage = 1;
this->points = 5;
}
Enemy::Enemy(float pos_x, float pos_y)
{
this->initVariables();
this->initTexture();
this->initSprite();
}
Enemy::~Enemy()
{
}
void Enemy::update() {
}
void Enemy::render(RenderTarget& target) {
target.draw(this->sprite);
}
**Enemy.h is:**
class Enemy
{
private:
Texture texture;
Sprite sprite;
int type;
int hp;
int hpMax;
int damage;
int points;
void initTexture();
void initSprite();
void initVariables();
public:
Enemy(float pos_x, float pos_y);
virtual ~Enemy();
void update();
void render(RenderTarget& target);
};
Подробнее здесь: https://stackoverflow.com/questions/784 ... es-in-sfml