嘿,我目前正在制作一个 sfml 平台游戏并打算使用地图图块,但是在实现我的地图类之后,它出现了一个未处理的异常。我先调用初始化函数,然后在最后调用drawmap。这是代码..
void Map::Initialise(const char *filename)
{
std::ifstream openfile(filename);
if(openfile.is_open())
{
std::string tempLine;
std::getline(openfile, tempLine);
tempLine.erase(std::remove (tempLine.begin(), tempLine.end(), ' '), tempLine.end());
MapX = tempLine.length();
openfile.seekg(0, std::ios::beg);
while(!openfile.eof())
{
openfile >> MapFile[loadCountX][loadCountY];
loadCountX++;
if(loadCountX >= MapX)
{
loadCountX = 0;
loadCountY++;
}
}
MapY = loadCountY;
}
}
void Map::DrawMap(sf::RenderWindow &Window)
{
sf::Shape rect = sf::Shape::Rectangle(0, 0, BLOCKSIZE, BLOCKSIZE, sf::Color(255, 255, 255, 255));
sf::Color rectCol;
for(int i = 0; i < MapX; i++)
{
for(int j = 0; j < MapY; j++)
{
if(MapFile[i][j] == 0)
rectCol = sf::Color(44, 117, 255);
else if (MapFile[i][j] == 1)
rectCol = sf::Color(255, 100, 17);
rect.SetPosition(i * BLOCKSIZE, j * BLOCKSIZE);
rect.SetColor(rectCol);
Window.Draw(rect);
}
}