0

我有那些继承类:基类:实体

派生自实体类:Actor、Obj、Enemy

基类实体包含一个用户定义类型的 obj,我称之为“CollisionStuff”。当我运行我的程序时, CollisionStuff 的析构函数在每次 CollisionStuff 构造函数调用之后以及每次游戏循环继续时都会被调用。

所以我的电话是:为什么会这样?

正如您在下面看到的,我在 setRectangle 方法中动态分配了一些数组,程序调用析构函数,它删除了我的数据,当我尝试使用它们时......它调用 "_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)); ”。

之前谢谢你

这是我的代码:Entity.h

enum e_Type {tActor = 0, tObj, tEnemy, tBackg};

class Entity
{
public:
    Entity(void);
    ~Entity(void);

    float getH();
    float getW();
    void setWH(float W, float H);
    bool CreateSprite(std::string path);
    sf::Sprite& getSprite();
    void setType(e_Type type);
    e_Type getType();
    CollisionStuff getColStuff();

    static std::list<Entity*> List;

protected:
    sf::Sprite m_sprite;
    sf::Texture m_texture;
    float m_h;
    float m_w;
    e_Type m_type;
    CollisionStuff m_colStuff;

    void addToList();
};

碰撞材料.h

class CollisionStuff
{
public:
    CollisionStuff();
    ~CollisionStuff(void);

    void setRectangle(int  W, int H);
    void followTheSprite(Entity entity);

private:
    sf::Vector2f* m_a;
    sf::Vector2f* m_b;
    sf::Vector2f* m_c;
    sf::Vector2f* m_d;
    /* this member data are sides of rectangle used
    to manage collisions between object throughout the scenario 

            a
      -------------
      |           |
    c |           | d
      |           |
      -------------
            b
    */

};

CollisionStuff.cpp

CollisionStuff::CollisionStuff()
{
    //setRectangle(0, 0);
}

void CollisionStuff::setRectangle(int W, int H)
{
    m_a = new sf::Vector2f[W];
    m_b = new sf::Vector2f[W];
    m_c = new sf::Vector2f[H];
    m_d = new sf::Vector2f[H];
}

void CollisionStuff::followTheSprite(Entity entity)
{
    entity.getSprite().setOrigin(0, 0);
    sf::Vector2f UpLeftVertex = entity.getSprite().getPosition();


    for(int i = 0; i <  entity.getW(); i++)
    {
        m_a[i].x = UpLeftVertex.x + i;
        m_a[i].y = UpLeftVertex.y;

        m_b[i].x = UpLeftVertex.x + i;
        m_b[i].y = UpLeftVertex.y + entity.getH();
    }

    for(int i = 0; i <  entity.getH(); i++)
    {
        m_c[i].x = UpLeftVertex.x;
        m_c[i].y = UpLeftVertex.y + i;

        m_d[i].x = UpLeftVertex.x + entity.getW();
        m_d[i].y = UpLeftVertex.y + i;
    }

}



CollisionStuff::~CollisionStuff(void)
{
    delete [] m_a;
    delete [] m_b;
    delete [] m_c;
    delete [] m_d;
}

编辑 谢谢你的回答。CollisionStuff 使用示例

Actor.cpp(它是实体的派生类)

Actor::Actor(void)
{
    if(!CreateSprite("D://Sprites//MainChar.png"))
    {
        std::cout << "Impossibile creare sprite" << std::endl;
    }
    else
    {
        std::cout << "Creazione sprite riuscita" << std::endl;  
        m_sprite.setPosition(100.0f, 365.0f);
        m_sprite.setOrigin(20, 35);
        //m_sprite.setPosition(190.0f, 382.5f); // 200, 400
        setWH(40, 70);
        m_health = 100;
        m_status = Good;
        setType(tActor);
        m_jCounter = -1;
        m_action = Null;

        setColStuff();
    }
}

void Actor::setColStuff()
{
    m_colStuff.setRectangle(m_w, m_h);
}



void Actor::physic()
{
    //setColStuff();
    m_colStuff.followTheSprite(*this);
}

主文件

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Platform");

    std::list<Entity*>::iterator i;

    Background BG;
    Level1 FirstLev;
    Actor Doodle; 

    while(window.isOpen())
    {
        sf::Event event;
        if(window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            Doodle.inputEvts();
        }

        Doodle.act(Doodle.getAction());

        Doodle.physic();

        window.clear();

        window.draw(BG.getSprite());
        window.draw(Doodle.getSprite());

        FirstLev.drawLevel(window);

        window.display();
    }
    return 0;
}
4

1 回答 1

2

从您发布的代码中很难判断,但如果我不得不猜测,我会说它可能与此有关:

CollisionStuff getColStuff();

CollisionStuff按值返回,这意味着调用它的人将创建一个新副本。它将具有与原始CollisionStuff对象分配的指针相同的指针,并且当它超出范围时它将删除它们,使原始对象具有悬空指针。

您可以尝试通过引用或指针返回,但无论哪种方式,您都应该编写一个复制构造函数并覆盖CollisionStuff三规则)的赋值运算符。

另一个想法是使用std::vector<sf::Vector2f>而不是自己分配sf::Vector2f数组。

于 2013-08-24T15:01:19.710 回答