2016-03-30 5 views
2

私は "AndroidゲームをビルドすることでJavaを学ぶ"を読んでおり、レトロスカッシュゲームの例ではラケットの衝突検出の実装方法はわかりません。ラケットの動きはonTouchEventを使用します。私はifステートメントを実装しようとしましたが、次のタッチイベントまでチェックされないので、正しく動作しません。助けてください。Android - レトロスカッシュゲーム - ラケット/スクリーン側の衝突

//Event that handles in which direction is the racket moving according to where is the user touching the screen 
    @Override 
    public boolean onTouchEvent(MotionEvent motionEvent) { 
     //gets the movement action without the pointers(ACTION_MASK) ??? -U: handles multitouch 

     switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { 
      //what happens if user touches the screen and holds 
      case MotionEvent.ACTION_DOWN: 
       //if the screen was touched on the right side of the display than move the racket right 
       if (motionEvent.getX() >= (screenWidth/2) && racketPosition.x <= screenWidth) { 
        racketIsMovingLeft = false; 
        racketIsMovingRight = true; 
       } else if (motionEvent.getX() < (screenWidth/2) && racketPosition.x >= 0) { 
        racketIsMovingLeft = true; 
        racketIsMovingRight = false; 
       } else { 
        racketIsMovingLeft = false; 
        racketIsMovingRight = false; 
       } 

       break; 
      //when the user lets go of the screen the racket immediately stops moving 
      case MotionEvent.ACTION_UP: 
       racketIsMovingLeft = false; 
       racketIsMovingRight = false; 
       break; 
     } 
     return true; 
    } 

答えて

0

です。私は解決策を見つけました。私はコードの正しい部分を見ていない - 申し訳ありません。ラケットのracketPoint.xを変更する部分を編集しました。ここでは、次のとおりです。

public void updateCount() { 
     //change the racket position according to the movement 
     if (racketIsMovingRight && (racketPosition.x+racketWidth/2)<=screenWidth) { 
      racketPosition.x += racketSpeed; 
     } 

     if (racketIsMovingLeft && (racketPosition.x-racketWidth/2)>=0) { 
      racketPosition.x -= racketSpeed; 
     } 

//コード

の残り