我正在使用SDL的RTS游戏。我有一个woodyard类,其目的是从附近的树木收集木材。在课堂上,我创建矢量称为temp_trees并作为构造函数的参数我用我通过在树对象的矢量C++把一个载体元素拖到另一个载体上
备料构造:
woodyard::woodyard(int x, int y, int HP, int id, vector<Tree> trees)
{
...
vector<Tree> temp_trees;
for(int i = 0; i < trees.size(); i++)
{
if((trees[i].xPos - 100)/50 >= x - 5 && (trees[i].xPos - 100)/50 <= x + 4)
{
if((trees[i].yPos - 100)/50 >= y - 5 && (trees[i].yPos - 100)/50 <= y + 4)
{
temp_trees.push_back(trees[i]);
}
}
}
collect_control = 0;
no = 0;
}
的collect_wood功能:
void woodyard::collect_wood(){
if(no == 5)
{
temp_trees[collect_control].drewno -= 1;
if(temp_trees[collect_control].drewno <= 0){
collect_control++;
temp_trees.erase(temp_trees.begin());
}}
no++;
if(no >= 10){
no = 0;
}}
程序在启动后崩溃。 任何人都可以看到这段代码中的任何错误?
PS:我想可能有一些错误的构造柯平从一个矢量元素到另一个。
你通过调试运行呢?调试器停止在哪里?它给了你什么错误? – kfsone
它停在 “temp_trees [collect_control] .drewno - = 1;” – user2466076
哪里是'temp_trees'宣布,WRT的'collect_wood'方法? – Massa