getCompatibleComponent返回向量错误
GetCompatibleComponent returning a Vector Error
我正在为大学项目创建一个基本按钮事件,我遇到了尝试访问实体的孩子的问题。
我正在使用一个组件模型来创建和显示屏幕上的所有对象,一个"按钮"由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();
相关文章:
- C++中函数的向量返回类型引发错误
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 如何使用C++初始化向量;脚本化值不是数组、指针或矢量错误
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 使用全局声明的向量时,C++双重释放错误/损坏
- 代码使用向量成功运行,但使用数组显示错误
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 全局向量导致 C++ 程序结束时出现段错误
- 为什么我在向量初始化时收到此错误?
- 在C++中比较来自向量的字符串时出现分段错误
- 将图形表示为unordered_map<字符串、向量>时拓扑排序错误<string>
- 为什么在向量上使用 std::find() 时会出现错误?
- 分割错误:向量中的擦除功能
- 编译错误向量排序和联合
- SIGBART 错误向量<列表<myClass>>
- 编译错误 ::向量 ::迭代器
- 为什么 std::copy 抛出错误向量迭代器 + 偏移量超出范围并且无法复制
- C++STL set_union获取错误向量迭代器+偏移量超出范围
- C++错误向量无法正确输出