我目前正在玩 RTS 游戏,我正在以这种方式从 txt 文件中加载单位:
1 1 700 200 10
/unit type/ /player/ /x co-ord/ /y co-ord/ /health/
我正在使用定居者的向量(我目前拥有的唯一单位)对象(不是指针)来保留所有单位
当我加载单元并尝试在屏幕上绘制它们时,它们不存在。我做了一些测试,即使我从 txt 文件加载,向量“定居者”也是空的
main.cpp 的代码:
vector<Settler> settlers;
...
void load_units(string filename)
{
settlers.clear();
ifstream unit_file(filename.c_str());
string line;
vector<vector<int> > ww;
while(unit_file.eof())
{
while(getline(unit_file, line))
{
stringstream ss(line);
int i;
vector<int> w;
while( ss >> i )
{
w.push_back(i);
}
ww.push_back(w);
}
}
for(int i = 0; i < ww.size(); i++)
{
int type = ww[i][0];
int player = ww[i][1];
int x = ww[i][2];
int y = ww[i][3];
int hp = ww[i][4];
if(type == 1)//settler
{
Settler settler(x, y, hp, player);
settlers.push_back(settler);
}
}
unit_file.close();}
...
void init() {
SDL_Init( SDL_INIT_EVERYTHING );
TTF_Init();
Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 );
SDL_WM_SetIcon(IMG_Load("icon.png"), NULL);
screen = SDL_SetVideoMode(1600, 900, 32, SDL_FULLSCREEN);
map = IMG_Load("tlo.png");
bar = IMG_Load("bar.png");
pause_menu = IMG_Load("menu.png");
save_game_menu = IMG_Load("save_game_menu.png");
save_prompt = IMG_Load("saved_prompt.png");
load_game_menu = IMG_Load("load_game_menu.png");
load_prompt = IMG_Load("loaded_prompt.png");
intro_control = true;
menu = true;
running = false;
paused = false;
saving = false;
loading = false;
mapX = 0;
mapY = 0;
Xoffset = 0;
Yoffset = 0;
load_map("mapa1.txt");
load_units("units1.txt");
for(int i = 0; i < 60; i++)
{
string file_frame;
stringstream ss;
ss << i + 1;
if(i < 9)
{
file_frame = ("intro/000");
}else
{
file_frame = ("intro/00");
}
file_frame.append(ss.str());
file_frame.append(".png");
intro[i] = IMG_Load(file_frame.c_str());
}}
...
int main( int argc, char* args[] ) {
int frame = 0;
Timer fps;
init();
Player p1(0, player_1_colour, "player 1");
int frame_control = 0;
while(intro_control)
{
......
PS:我怀疑“while(unit_file.eof()){”语句可能有问题。但这只是一个猜测