我一直试图让这个工作一段时间,我对C ++ / CLI不是很熟练。我试图循环一个2d数组,当它包含一定数量创建一个线程运行但我在编译时不断收到错误。 这是线程创建:
if (map[x][y] == 8)
{
Pos^ p = gcnew Pos(x, y, map);
Thread^ t = gcnew Thread(gcnew ParameterizedThreadStart(p, &Pos::moverX));
t->Start(p);
}
else if (map[x][y] == 9)
{
Pos^ p = gcnew Pos(x, y, map);
Thread^ t = gcnew Thread(gcnew ParameterizedThreadStart(p, &Pos::moverY));
t->Start(p);
}
这是Pos类:
public ref class Pos
{
public:
static int px, py;
static int ** mapa;
Pos(int x, int y, int ** map)
{
px = x;
py = y;
mapa = map;
}
int getX(){ return px; }
int getY(){ return py; }
int** getMap(){ return mapa; }
static void moverX(Pos p)
{
int dy = 1;
while (true)
{
if (mapa[p.getX()+dy][p.getY()] == 1){ dy *= -1; }
Console::BackgroundColor = ConsoleColor::Black;
Console::SetCursorPosition(p.getY() + 30, p.getX() + 5);
cout << " ";
Console::SetCursorPosition(p.getY() + 30, p.getX() + 5+dy);
cout << (char)164;
Thread::Sleep(1000);
}
}
static void moverY(Pos p)
{
int dy = 1;
while (true)
{
if (mapa[p.getX()][p.getY() + dy] == 1){ dy *= -1; }
Console::BackgroundColor = ConsoleColor::Black;
Console::SetCursorPosition(p.getY() + 30, p.getX() + 5);
cout << " ";
Console::SetCursorPosition(p.getY() + 30 + dy, p.getX() + 5);
cout << (char)164;
Thread::Sleep(1000);
}
}
};