【发布时间】:2022-10-20 18:50:29
【问题描述】:
所以我试图用 SDL2 用 C++ 制作一个游戏,但遇到了一个小问题。为了从地上捡起武器,在我检查玩家是否在对象旁边并单击“E”从不同命名空间的内联函数中捡起它之后,我必须将其地址复制到一个变量中保存在主类中并从地面删除对象,这样您就无法重新拾取它。似乎在我删除对象后,来自主类的指针从另一个对象获取值,而不是我擦除的值。
这是主类 .h 文件:
class GameScene : public Scene{
public:
GameScene();
void init(SDL_Renderer* &renderer, int sceneIdx); //initialize variables
void update(int &sceneIdx, double deltaTime, Vector2f screenSize); //update frame-by-frame
void graphics(SDL_Renderer* &renderer); //render objects
void clear(); //only called on exit
private:
std::vector<SDL_Texture*> textures = {}; //all the textures present in the game
std::vector<bool> movements{false, false, false, false}; //main character movements
std::vector<Weapon> droppedWeapons = {}; //dropped pickable weapons on the ground
std::vector<Weapon*> weapons = {nullptr, nullptr}; //slots for the primary and secondary weapon
std::vector<Bullet> bullets = {}; //ssaves all the fired bullets on the map until deletion
std::unordered_map<int, SDL_Rect> positionsAtlas = {}; //offsets in textures and render scales
Cube* cube = nullptr; //main character
int mode = 0; //0=nothing, 1=weapon --unused yet
bool currentWeapon = 0; //saves what weapon is being used(primary or secondary)
int mouseX, mouseY; //mouse position on screen
};
这里是函数调用.cpp 文件:
WeaponActions::pickUpWeapons(cube, droppedWeapons, weapons, pickUp/*is E pressed*/, currentWeapon);
和中的功能WeaponActions 命名空间:
inline void pickUpWeapons(Cube* cube, std::vector<Weapon> &droppedWeapons, std::vector<Weapon*> &weapons, bool pickUp, bool ¤tWeapon)
{
for(unsigned int i=0;i<droppedWeapons.size();i++)
{
bool type = droppedWeapons[i].getType(); //type of weapon(primary or secondary)
if(weapons[type]==nullptr && pickUp) //there is empty space in inventory
{
if(Math::checkCollision(cube->getPos(), cube->getScale(), droppedWeapons[i].getPos(), droppedWeapons[i].getScale())) //check if cube is near weapon
{
weapons[type] = &droppedWeapons.at(i); //save address
droppedWeapons.erase(droppedWeapons.begin()+i); //delete element
currentWeapon = currentWeapon == type ? currentWeapon : type; //change current weapon if necessary
i--;
}
}
}
}
Weapon 对象中的 type 元素表示它是主要(步枪)还是次要(手枪)武器。 除了创建另一个对象来存储指针指向的对象之外,我应该怎么做?
【问题讨论】:
-
您可以简单地将所有武器存储在包含所有武器数据的单个向量中,无论它是在地面上、库存中还是其他地方。只需使用指针向量或
std::reference_wrappers 来存储地面上的物品列表,在库存或其他地方。请注意,您需要确保包含实际数据的向量永远不需要重新分配它的后备存储,并且永远不会移动项目以使向量中的地址保持稳定。到目前为止,最简单的方法是简单地存储指向项目的共享指针,但要注意内存碎片 -
你知道指针不存储对象吗?它只是指向存储在其他地方的对象。
-
我在餐巾纸上写下我的地址“123 Main St”,然后交给房地产经纪人出售。然后因为他有地址,我不再需要房子,所以我签了拆迁令。但是当房地产经纪人来看我的房子时,他说那是一堆瓦砾。我告诉他那是胡说八道——我拆掉了房子后我给了他地址。
-
在编写指向存储的指针时,您会发现类似的问题。如果您写入指针,则很可能在读回存储空间并恢复指针时,它指向的对象将消失。您几乎总是需要编写对象本身。