2012-03-16 36 views
0

私は簡単な作業をしようとしています。私は、画面の左上隅から円が始まっています。画面上のどこかをタッチすると、円が、ユーザーが触った場所に向かってゆっくりと移動して止まるようにしたいと思います。私はこれをうまく実装しましたが、円はあまりにも速く動いています。私は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--; 

      } 

     } 
    } 
+0

実行ループにThread.sleep()を追加することは役に立ちませんか?そして、各ビットマップ描画の後にunlockAndPostを実行すると、コードスニペットからエスケープされただけです。 –

+0

はい、いくつかの時間間隔でThread.sleep()を試しましたが、それは効果がないようです。そして、はい、私はunlockAndPost()メソッドを持っていますが、私は偶然にそれを放棄しました – Derek

答えて

0

[OK]を実行している問題は、経過時間を考慮していないことです。これはまた、アニメーションが異なるデバイス上で異なる速度で移動し、異なる動作がバックグラウンドで実行されることを意味します。あなたがする必要があるのは、円を移動させたいミリ秒あたりのピクセル数を決定し、最後のフレームからの経過時間によって移動距離を変更することです。

+0

リニア補間も役立つかもしれません。 – triggs

関連する問題