Я пытаюсь выполнить простую задачу. У меня есть круг, начинающийся в верхнем левом углу экрана. Когда я касаюсь экрана в любом месте, я хочу, чтобы круг медленно двигался в направлении и останавливался на месте, к которому прикоснулся пользователь. Я успешно реализовал это, однако круг движется слишком быстро. Я считаю, что мне нужно попытаться перевести мой поток run () в спящий режим на несколько миллисекунд, но я не могу успешно это сделать. Каково было бы решение этого? Код прост, но я выложил ниже на всякий случай, заранее спасибо за помощь!
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public GameView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
public void run()
{
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
int screenArrayWidth = screenWidth / 50;
int screenArrayHeight = screenHeight / 30;
int[][] mapArray = new int[50][30];
while (isItOK)
{
if (!holder.getSurface().isValid())
{
continue;
}
c = holder.lockCanvas();
c.drawARGB(255, 255, 0, 0);
c.drawBitmap(ball, destinationX - (ball.getWidth() / 2),
destinationY - (ball.getHeight() / 2), null);
}
}
public boolean onTouch(View v, MotionEvent me)
{
switch (me.getAction())
{
case MotionEvent.ACTION_DOWN:
{
while (((int)(me.getX()) != (int)(destinationX)) && ((int)(me.getY()) != (int)(destinationY)))
{
if (me.getX() > destinationX)
{
destinationX++;
}
if (me.getX() < destinationX)
{
destinationX--;
}
if (me.getY() > destinationY)
{
destinationY++;
}
if (me.getY() < destinationY)
{
destinationY--;
}
}
}