#include "solarsystem.h"
#include "planet.h"
#include <vector>
static int number_of_planets, planet_type;
String planet;
String planet_id;
static std::vector<Planet> allPlanets(number_of_planets);
void Solarsystem::planets()
{
allPlanets.size();
srand(time(NULL));
number_of_planets = rand() % 8 + 1;
for (int i = 0; i <= number_of_planets; i++)
{
srand(time(NULL));
planet_type = rand() % 100 + 1;
if (i <= 2)
{
if (planet_type >= 0)
{
planet_id = "barren";
}
if (planet_type >= 31)
{
planet_id = "gas";
}
if (planet_type >= 71)
{
planet_id = "desert";
}
}
else if (i <= 5)
{
if (planet_type >= 0)
{
planet_id = "barren";
}
if (planet_type >= 21)
{
planet_id = "gas";
}
if (planet_type >= 41)
{
planet_id = "ocean";
}
if (planet_type >= 61)
{
planet_id = "continental";
}
if (planet_type >= 81)
{
planet_id = "jungle";
}
}
else if (i <= 8)
{
if (planet_type >= 0)
{
planet_id = "barren";
}
if (planet_type >= 31)
{
planet_id = "gas";
}
if (planet_type >= 71)
{
planet_id = "ice";
}
}
allPlanets[i] = Planet();
}
}
void Solarsystem::render(RenderWindow &window)
{
if (inside == true)
{
if (a == true)
{
planets();
a = false;
}
planet = planet_id;
for (int z = 1; z <= number_of_planets; z++)
{
int x = 700;
int y = 100;
if (z == 1)
{
allPlanets[z].planet_spr.setOrigin(700/2, 700/2);
allPlanets[z].planet_spr.setPosition(0, 0);
}
else
{
allPlanets[z].planet_spr.setOrigin(275/2, 275/2);
allPlanets[z].planet_spr.setPosition(x, y);
int x = x + 700;
int y = y + 100;
}
allPlanets[z].render(window, planet);
}
}
else if(inside == false)
{
solarsystem_txt.loadFromFile("solarsystem.png");
solarsystem_spr.setTexture(solarsystem_txt);
window.draw(solarsystem_spr);
}
}
你好,我有一個問題,每次我想開始我的PROGRAMM我得到的錯誤:「表達式:向量下標越界」C++(SFML)向量下標越界
錯誤是lockated在這裏:
if (size() <= _Pos)
{ // report error
_DEBUG_ERROR("vector subscript out of range");
_SCL_SECURE_OUT_OF_RANGE;
}
(不是我的代碼)
我只是用矢量開始,我不知道我做錯了什麼。
您正試圖訪問比您的矢量大的索引。檢查你在看'allPlanets [z]'也許。另外,查看錯誤的堆棧跟蹤,然後回到導致問題的代碼行。它也可能值得一看像'std :: map',用於將你的行星串id映射到整數ID。 – danielunderwood
這個'for(int i = 0; i <= number_of_planets; i ++)'應該是for(int i = 0; i
Galik
感謝您的幫助。我會查找std :: map – user6395356