C++ 将字符转换为字符串不';t工作良好C++;
我有这样的密码:C++ 将字符转换为字符串不';t工作良好C++;,c++,arrays,char,sfml,C++,Arrays,Char,Sfml,我有这样的密码: 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::Rectan
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);
}
}
如果有人想要保留代码,则有:
谢谢P
只是猜测,因为没有足够的信息来确定,但这可能就是答案
std::string ruta(int 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";
}
在C++中,你必须小心类型,并且理解差异,例如,在<代码> int >代码>和<代码> char < />代码之间。值为“3”的字符与值为3的int不同。只是猜测,因为没有足够的信息可以确定,但这可能就是答案
std::string ruta(int 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";
}
在C++中,你必须小心类型,并且理解差异,例如,在<代码> int >代码>和<代码> char < />代码之间。值为“3”的字符与值为3的int不同。我立即看到的一个问题是,您正在将
int
与char
进行比较。考虑:
std::string ruta(int id)
{
switch( id )
{
case 0:
return "air";
case 1:
return "stone";
case 2:
return "dirt";
case 3:
return "grass_side";
case 4:
return "coal_ore";
}
}
我立即看到的一个问题是,您正在将
int
与char
进行比较。考虑:
std::string ruta(int id)
{
switch( id )
{
case 0:
return "air";
case 1:
return "stone";
case 2:
return "dirt";
case 3:
return "grass_side";
case 4:
return "coal_ore";
}
}
以下是您的现场声明:
int scene[worldWidth][worldHeight];
以下是如何填充场景:
while (!finished) {
if (yPos >= topOfTheWorld) {
scene[xPos][yPos] = 1;
}
else if(yPos < topOfTheWorld) {
scene[xPos][yPos] = 0;
}
//etc...
}
while(!finished){
如果(yPos>=topOfTheWorld){
场景[xPos][yPos]=1;
}
否则如果(yPos
以下是如何写入mapa.txt文件:
std::ofstream output("mapa.txt");
for(int y=0;y<worldHeight;y++) {
for(int x=0;x<worldWidth;x++) {
output<<scene[x][y];
if(x<(worldWidth-1)){output<<",";}
}
if(y<(worldHeight-1)){output<<std::endl;}
}
std::of流输出(“mapa.txt”);
对于(int y=0;y,以下是您的场景声明:
int scene[worldWidth][worldHeight];
以下是如何填充场景:
while (!finished) {
if (yPos >= topOfTheWorld) {
scene[xPos][yPos] = 1;
}
else if(yPos < topOfTheWorld) {
scene[xPos][yPos] = 0;
}
//etc...
}
while(!finished){
如果(yPos>=topOfTheWorld){
场景[xPos][yPos]=1;
}
否则如果(yPos
以下是如何写入mapa.txt文件:
std::ofstream output("mapa.txt");
for(int y=0;y<worldHeight;y++) {
for(int x=0;x<worldWidth;x++) {
output<<scene[x][y];
if(x<(worldWidth-1)){output<<",";}
}
if(y<(worldHeight-1)){output<<std::endl;}
}
std::of流输出(“mapa.txt”);
对于(int y=0;yy您正在将int-id与char进行比较,如果id
不是这些东西呢?(您是否收到任何编译器警告?)此代码将效率低下,因为每次调用ruta()都会创建一个要返回的新字符串。您最好创建一个数组,const std::string ruta[]={“air”、“stone”、“dirty”,“grass_side”、“coal_ore”};
并使用ruta[mapa[y][x]]。您将int-id与char进行比较时,可能会出现重复。如果id
不属于这些内容呢?(您收到任何编译器警告吗?)此代码将效率低下,因为每次调用ruta()它会创建一个新字符串返回。最好创建一个数组,const std::string ruta[]={“air”、“stone”、“dirty”、“grass\u side”、“coal\u ore”};
并使用ruta[mapa[y][x]].Ah的可能重复!我忘了输入字符id,但它仍在崩溃…:/Ah!我忘了输入字符id,但它仍在崩溃…:/我忘了在函数参数中将“int”更改为“char”,但它仍在崩溃,我不知道为什么…:/if(!texture.loadFromFile(“images/blocks/”+ruta(mapa[y][x])+.png”)这是错误的
应该是如果(!texture.loadFromFile(“images/blocks/”+ruta(mapa[x][y])+“.png”)
你把x和y搞错了。这个char mapa[worldWidth-1][worldWidth-1];
看起来不对。为什么不明显的char mapa[worldWidth][worldWidth]
?您可能有很好的理由,但我怀疑这只是一个错误。@Ikillnukes不,我没有说相同的代码,您的x和y是错误的。@Ikillnukes您的代码中可能有很多错误。您应该使用调试器。我忘了在函数参数中将“int”更改为“char”,但它仍然崩溃,我不知道为什么…:/这是错误的if(!texture.loadFromFile(“images/blocks/”+ruta(mapa[y][x])+“.png”)
应该是if(!texture.loadFromFile(“images/blocks/”+ruta(mapa[x][y])+“.png”)
。你把x和y搞错了。这char mapa[worldWidth-1][worldWidth-1];
看起来是错误的。为什么不明显的char mapa[世界宽度][世界高度]
?您可能有很好的理由,但我怀疑这只是一个错误。@Ikillnukes不,我没有说相同的代码,您的x和y是错误的。@Ikillnukes您的代码中可能有很多错误。您应该使用调试器。我正按照您所说的编写,但是当我对所有映射进行充电时,它会作为字符而不是int进行充电,所以。。。我必须将其作为一个字符进行比较,而不是作为一个整数进行比较…但是如果我这样做,应用程序仍然会崩溃…:/我正在写这篇文章,正如你所说,但是当我对所有地图进行充电时,它会作为一个字符进行充电,而不是作为一个整数,因此…我必须将其作为一个字符进行比较,而不是作为一个整数进行比较…但是如果我这样做,应用程序仍然会崩溃…:/