getCompatibleComponent返回向量错误

GetCompatibleComponent returning a Vector Error

本文关键字:错误 向量 返回 getCompatibleComponent      更新时间:2023-10-16

我正在为大学项目创建一个基本按钮事件,我遇到了尝试访问实体的孩子的问题。

我正在使用一个组件模型来创建和显示屏幕上的所有对象,一个"按钮"由3个单独的组件组成,一个单独的组件,一个显示按钮所做的内容,即"启动游戏"并在文本上方显示图像,以及一个充当我按钮的"边界框"的ShapeComponent。该组件纯粹是一种事件处理程序,检查鼠标是否在形状的边界框中,如果是的,则允许用户与之交互(例如显示消息(。

以下是我的一个按钮的代码。

static shared_ptr<Entity> btnStartGame;
void MainMenuScene::Load() {

    //START GAME BUTTON
    auto txtNewGame = makeEntity();
    auto t = txtNewGame->addComponent<TextComponent>("New Game");
    t->getText().setOrigin(t->getText().getGlobalBounds().width / 2, t->getText().getGlobalBounds().height / 2);
    txtNewGame->setPosition(Vector2f(280.f, 500.f));

    auto sword = makeEntity();
    auto s = sword->addComponent<SpriteComponent>();
    s->Sprite("Sword.png", IntRect(0, 0, 60, 60));
    sword->setPosition(Vector2f(370.f, 540.f));

    auto btnStartGame = makeEntity();
    auto b = btnStartGame->addComponent<ShapeComponent>();
    b->setShape<sf::RectangleShape>(Vector2f(200.f, 105.f));
    b->getShape().setFillColor(Color(224, 190, 20));  //just so I can see where it is
    b->getShape().setOrigin(b->getShape().getGlobalBounds().width / 2, b->getShape().getGlobalBounds().height / 2);
    btnStartGame->setPosition(Vector2f(Engine::GetWindow().getSize().x / 4 - 40.f, 525.f));
}

创建了边界框后,我将在更新功能中进行验证检查,以查看鼠标当前是否在ShapeComponent的边界框中。我获得了btnstartgame entity的组件并获得其全球播放,完成此操作后,我检查鼠标当前是否在框中,但是此行会导致错误。

if (btnStartGame->get_components<ShapeComponent>()[0]->getShape().getGlobalBounds().contains(Engine::GetWindow().mapPixelToCoords(sf::Mouse::getPosition())))
    {
        cout << "level 1 selected" << endl;
    }

我在Vector

中的此行读取访问违规错误
pointer _Unchecked_begin() _NOEXCEPT
    {   // return pointer for beginning of mutable sequence
    return (this->_Myfirst());
    }

有人知道为什么会发生这种情况吗?我一开始认为这是一个指针错误,但似乎没有解决任何问题。

解决了问题。

我创建了一个共享_ptr,而不是引用我创建的实体,而是试图指向load((函数中的新实体。

从btnstartgame中删除自动解决问题

btnStartGame = makeEntity();