【发布时间】:2020-04-07 13:14:48
【问题描述】:
我目前正在使用 C++ 和 SFML 制作 Space Invaders 的克隆,并且似乎对玩家的位置有疑问。
目前,当入侵者射击时,玩家的hit-box继续是玩家的起始位置,记录为900、500。这个hit-box不会移动玩家。我不确定为什么以及如何解决它。我知道我不能将 game.h 包含到inavivers.h 中,因为 game.h 包含了invenders.h,因此会导致包含循环。我需要获取在game.h中创建的玩家实例,并将其传递给inviders.h,以便玩家的hit-box也被传递。
这是与入侵者射击和玩家位置相关的代码。
游戏.h
#include "Player.h"
#include "invaders.h"
class Game
{
private:
Player* player;
vector<Invaders*> vInvaders;
public:
void updateInputs();
};
游戏.cpp
#include "Game.h"
void Game::updateInputs()
{
//Updates player movement.
player->updateInputs();
//Creating bullets.
if ((Keyboard::isKeyPressed(Keyboard::Space)) && (this->player->bCanAttack()))
{
this->vBullets.push_back(new Bullet(this->textures["BULLET"], this->player->getPos().x, this->player->getPos().y, 0.f, -1.f, 5.f));
SoundEngine::playShot();
}
//Move player.
if (Keyboard::isKeyPressed(Keyboard::Left))
{
move(-1.f, 0);
getBounds();
//cout << player->getBounds().left << " " << player->getBounds().top << endl;
}
if (Keyboard::isKeyPressed(Keyboard::Right))
{
move(1.f, 0);
getBounds();
//cout << player->getBounds().left << " " << player->getBounds().top << endl;
}
}
播放器.h
class Player
{
public:
const FloatRect getBounds() const;
void move(const float kfDirX, const float kfDirY);
};
播放器.cpp
#include "Player.h"
const FloatRect Player::getBounds() const
{
return this->sprite.getGlobalBounds(); //Returns the bounding box of player.
}
void Player::move(const float kfDirX, const float kfDirY)
{
this->sprite.move(this->fSpeed * kfDirX, 0); //Moves player across screen.
}
入侵者.h
#include "Player.h" //It is not needed, but doesn't work without it.
class Invaders
{
private:
Player player; //It is not needed, but does't work without it. Need the instance from Game.h, don't need to make a new instance here.
public:
void updateBullets();
};
入侵者.cpp
#include "Invaders.h"
void Invaders::updateBullets()
{
unsigned int iCounter = 0;
for (auto* invaderBullet : this->vInvaderBullets)
{
invaderBullet->update();
for (size_t k = 0; k < this->vInvaderBullets.size(); k++)
{
//THIS DOES NOT WORK!!!!
if (this->vInvaderBullets[k]->getBounds().intersects(player.getBounds()))
{
cout << player.getBounds().left << " " << player.getBounds().top << endl;
this->vInvaderBullets.erase(this->vInvaderBullets.begin() + k);
this->player.isDead(true);
Variables::iLives--;
}
else
{
this->player.isDead(false);
}
}
//Bullet culling at bottom of screen.
if ((invaderBullet->getBounds().top + invaderBullet->getBounds().height) > 1100.f)
{
//std::cout << this->invaderBullets.size() << std::endl;
delete this->vInvaderBullets.at(iCounter);
this->vInvaderBullets.erase(this->vInvaderBullets.begin() + iCounter);
iCounter--;
//Check to see if bullets are deleted.
//std::cout << this->invaderBullets.size() << std::endl;
}
iCounter++;
for (size_t k = 0; k < this->vInvaderBullets.size(); k++)
{
if (this->vInvaderBullets[k]->getBounds().intersects(this->barrier.getBounds(1)))
{
SoundEngine::playBarrierHit();
this->vInvaderBullets.erase(this->vInvaderBullets.begin() + k);
this->barrier.barrierHit(1);
}
else if (this->vInvaderBullets[k]->getBounds().intersects(this->barrier.getBounds(2)))
{
SoundEngine::playBarrierHit();
this->vInvaderBullets.erase(this->vInvaderBullets.begin() + k);
this->barrier.barrierHit(2);
}
else if (this->vInvaderBullets[k]->getBounds().intersects(this->barrier.getBounds(3)))
{
SoundEngine::playBarrierHit();
this->vInvaderBullets.erase(this->vInvaderBullets.begin() + k);
this->barrier.barrierHit(3);
}
else if (this->vInvaderBullets[k]->getBounds().intersects(this->barrier.getBounds(4)))
{
SoundEngine::playBarrierHit();
this->vInvaderBullets.erase(this->vInvaderBullets.begin() + k);
this->barrier.barrierHit(4);
}
}
}
}
【问题讨论】:
-
您是否尝试过使用
#pragma once来打破包含循环? -
我有,但它无法解决循环
-
您的代码不存在任何包含循环。 3 个标头中的任何一个都不需要任何其他标头才能工作,但很明显它们并不完整。请提供minimal reproducible example(
.cpp文件在这里可能没用,可以省略) -
这将有助于循环包括:stackoverflow.com/questions/625799/…
-
我需要获取在 game.h 中创建的玩家实例,并将其传递给inviders.h,以便同时传递玩家的命中框。它现在编译的唯一原因是因为我在 inavders.h 中创建了一个新的 Player 实例。