【发布时间】:2013-09-20 20:21:16
【问题描述】:
我有那个代码:
for (int x = 0; x<(worldWidth-1); x++) {
for (int y = 0; y<(worldHeight-1); y++) {
sf::Texture texture;
if (!texture.loadFromFile("images/blocks/" + ruta(mapa[y][x]) + ".png"))
return -1;
sf::RectangleShape rectCaja(sf::Vector2f(16, 16));
rectCaja.setPosition(sf::Vector2f(x*16, y*16));
rectCaja.setTexture(&texture);
window.draw(rectCaja);
}
}
打印框(16*16 像素),在游戏中是“块”,问题是它不打印任何块,它直接崩溃我不知道为什么:/
我知道(通过控制台测试)数组“mapa”没有错......所以我唯一的解释是 ruta 函数不能正常工作......(我已经用标准测试了它: :string var = "dirt"; 它工作正常)... :/
std::string ruta(char id) {
if (id=='0') return "air";
if (id=='1') return "stone";
if (id=='2') return "dirt";
if (id=='3') return "grass_side";
if (id=='4') return "coal_ore";
}
如果有人想要保留代码:http://pastebin.com/5jvbzwkR
谢谢! :P
【问题讨论】:
-
您将 int id 与 char 进行比较
-
如果
id不是这些东西怎么办? (你有没有收到任何编译器警告?) -
这段代码效率很低,因为每次调用 ruta() 它都会创建一个新的字符串来返回。您最好创建一个数组
const std::string ruta[] = { "air", "stone", "dirt", "grass_side", "coal_ore" };并使用 ruta[mapa[y][x]]。