-1

我正在开发一款 roguelike 游戏,我将其设置为在 MOST 一个关卡上会有 3 个怪物。所以我有3个指向怪物的指针。它们都以 NULL 开始。

在那个怪物死后,我应该如何将指针设置回 NULL?或者,也许我有错误的想法?本质上,我需要移除怪物,直到该指针重新生成为另一个怪物。

我已经尝试了一些事情,但我只是不断遇到段错误。

我尝试使用擦除、删除,只需将其设置为 NULL,然后重新指向它。想不通。

这是我设置指针的方式:

CreatureFactory myCreatures = CreatureFactory::instance();
    Creature * pCreature1 = NULL;
    Creature * pCreature2 = NULL;
    Creature * pCreature3 = NULL;;

    ItemFactory myItems = ItemFactory::instance();
    Item * pItem1 = myItems.generateItem();

    dl.placeInGame(personPlaying,randomGen);
    int monsterCount = 0;
    int turnCount = 0;
    bool playingGame = true;
    char playerController;

    while (playingGame){
            if ((turnCount % 2 == 0) && (personPlaying.getHP() < personPlay$
                    personPlaying.generateHP();
            }
            if (((turnCount % 3) == 0) && (monsterCount < 2)){
                    if(pCreature1 == NULL){
                            pCreature1 = myCreatures.generateCreature(4);
                            dl.placeInGame(pCreature1,randomGen);
                            monsterCount += 1;
                    }
                    else if(pCreature2 == NULL){
                            pCreature2 = myCreatures.generateCreature(4);
                            dl.placeInGame(pCreature2, randomGen);
                            monsterCount += 1;
                    }
                    else if(pCreature3 == NULL){
                            pCreature3 = myCreatures.generateCreature(4);
                            monsterCount += 1;
                    }
            }
            dl.dump();
            personPlaying.dumpStats();

 CreatureFactory code..
 CreatureFactory & CreatureFactory::instance(){
    static CreatureFactory myObj;
    return myObj;
}

CreatureFactory::CreatureFactory(){
    m_mtRandom.seed( time(NULL) );
    fstream xmlFile;
    xmlFile.open("critters.xml");
    vector<XMLSerializable*> pObjects;
    parseXML(xmlFile, pObjects);

    XMLSerializable * pObject;

    for(auto it = pObjects.begin(); it != pObjects.end(); it++){
            pObject = (*it);
            Creature * pCreature = dynamic_cast<Creature*>(pObject);
            if (pCreature != NULL){
                    m_vCreatures.push_back(pCreature);
            }
    }
}

CreatureFactory::~CreatureFactory(){

}
    Creature * CreatureFactory::generateCreature(int iMaxLevel) {
    vector<Creature*> tempCreatures;

    for(auto it = m_vCreatures.begin(); it != m_vCreatures.end(); it++){
            if ((*it)->getLevel() <= iMaxLevel){
                    tempCreatures.push_back((*it));
            }
    }
    int randomCreature = (m_mtRandom() % (tempCreatures.size() - 1));


    Creature * pCreature = tempCreatures.at(randomCreature);

    Creature * pReturnValue = new Creature(*pCreature);

    return pReturnValue;
}

然后我做了大量的循环并使用了大量的类。指向这些怪物对象的指针通过大约 6 或 7 个类传递。当 HP 达到 0 时,我需要将其中之一设置为 NULL。

谢谢

4

1 回答 1

4

在 c++ 中,您不应该将指针设置为 null,因为 Object 仍将存在于内存中,但没有对它的任何引用-> 内存泄漏。相反,您应该删除对象:

delete pCreatureN;

这基本上会从内存中删除对象并使指针成为空指针。之后pCreatureN = NULL;就好了。

于 2013-04-24T08:54:38.180 回答