我目前正在研究C ++和SFML的“太空侵略者”的克隆,似乎对玩家的位置有问题。
[当前,当入侵者射击时,播放器的打击框继续保持为记录为900、500的播放器的起始位置。此打击框不会移动播放器。我不确定为什么以及如何解决它。我知道我不能将game.h包含到invaders.h中,因为game.h包含invaders.h会导致包含循环。我需要使用在game.h中创建的玩家实例,并将其传递给invaders.h,以传递玩家吨的点击框。
这是与侵略者投篮和球员位置有关的代码。
Game.h
#include "Player.h"
#include "invaders.h"
class Game
{
private:
Player* player;
vector<Invaders*> vInvaders;
public:
void updateInputs();
};
Game.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;
}
}
Player.h
class Player
{
public:
const FloatRect getBounds() const;
void move(const float kfDirX, const float kfDirY);
};
Player.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.
}
Invaders.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();
};
Invaders.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);
}
}
}
}
在Player
中放入类型为Invaders
的成员表示它是一个单独的对象,可能与Player
使用的Game
不同。因此,最有可能的玩家移动发生在Game
的Player
上,但入侵者的目标是他们自己独特的Player
,但没有移动。
由于Invaders::updateBullets()
需要Player
对象,因此有几个选项:
Player&
或Game&
传递到updateBullets
功能。Game
为单例,并通过公共方式从任何上下文中获取Game
对象,并因此获得其Player
对象。Player*
中的Game*
或Invaders
指针-但是如果这些对象可以在Invaders
对象之前被销毁,则处理起来会很棘手。这可能有点“浪费”。并且通常,请记住,如果您只处理指向该类的指针或引用,则不需要类定义,这可以帮助减少所需的#include
指令。例如,您可以更改Game.h和Game.cpp:
// Game.h
#ifndef GAME_H
#define GAME_H
#include <vector>
class Player;
class Invaders;
class Game
{
private:
Player* player;
std::vector<Invaders*> vInvaders;
public:
void updateInputs();
};
#endif
// Game.cpp
#include "Game.h"
#include "Player.h"
#include "Invaders.h"
// ...