2012-02-24 3 views
7

私は現在、ユーザーが画面の左側のジョイスティックで画面を操作する船をコントロールし、画面の右側をタップするとゲームを開始しています。しかし、私のマルチタッチの試みは成功していません。ユーザーがジョイスティックに最初に触れ、次に発射すると、それは完全に機能するが、発射中にユーザーがスティックに触れると、ユーザーはジョイスティックをドラッグできなくなる。私はまだアンドロイドには新しいので、どんな助けでも大歓迎です。私は以下のコードを貼り付けます。Androidマルチタッチゲーム

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    int action = event.getAction() & MotionEvent.ACTION_MASK; 
    int pointerIndex=(event.getAction()&MotionEvent.ACTION_POINTER_ID_MASK) 
    >>MotionEvent.ACTION_POINTER_ID_SHIFT; 
    int pI = event.getPointerId(pointerIndex); 
    //On screen joystick 
    if(event.getX(pI)<pad.getWidth()+(screenWidth*.18)&&event.getX(pI)>0&&event.getY(pI)<(screenHeight)&&event.getY(pI)>(screenHeight-(0.42*screenHeight))){ 
     sx2=event.getX(pI)-(stick.getWidth()/2); 
     sy2=event.getY(pI); 
     angle=(Math.atan2((sy2-sy),(sx2-sx))); 
    } 
    //firing system 
    if(event.getX(pI)>screenWidth/3){ 
     switch(action){ 
     case MotionEvent.ACTION_DOWN: 
      incrementRunnable.run(); 
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      incrementRunnable.run(); 
      break; 
     case MotionEvent.ACTION_UP: 
      handler.removeCallbacks(incrementRunnable); 
      break; 
     case MotionEvent.ACTION_POINTER_UP: 
      handler.removeCallbacks(incrementRunnable); 
      break; 
     } 

    } 
    //reset joystick 
    if(event.getAction()==MotionEvent.ACTION_UP||event.getAction()==MotionEvent.ACTION_POINTER_UP){ 
     sx2=sx; 
     sy2=sy; 
     handler.removeCallbacks(incrementRunnable); 
    } 
    return true; 
} 
+0

それは、ハードウェアのことだ場合は試してみてください。 Googleデバイスの中には、マルチタッチに奇妙な問題があるものがあります。https://play.google.com/store/apps/details?id=multitouchpro.tests&hl=de – devsnd

+0

ハードウェアの問題ではないと思います。マルチタッチは私の電話でうまくいきます。私は銀河のs2でそれをテストしてきた – user1229888

答えて

1

あなたが孤立して、ジョイスティックや火災のボタンを操作できることを考えると、それはあなたが正しく画面サイズを取得しているし、あなたのゲームオブジェクトへのポインタの周辺を検出に関する他のすべてが正しいことを理にかなっています。私は特定のAndroidバージョンとマルチタッチの問題について読んだことがあるが、それは問題ではないと仮定しよう。

MotionEventのトリッキーなことは、複数のポインタのために解雇されていることです。タッチや移動に興味がある場合は、すべてのポインタを考慮する必要があります。問題はあなたのアプローチにあると思う。

さまざまな種類のイベントがある場合、ユーザーが何をタッチしているのかを判断してから、どのような種類のイベントが発生しているのかを把握し、ユーザーがどのオブジェクトを操作しているのかを判断する必要があります。これにより、コードが少しきれいになり、将来問題が見つけやすくなります。

私は上記の例をとり、以下のように書き直しました。この例では、ユーザーがタッチしている場所と場所を示すテキストを画面に表示します。それは、火災ボタンの後にジョイスティックの接触を決定することができ、その逆も可能である。

MultiTouchGame.java

package com.droidhut.stackoverflow; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.WindowManager; 
import android.widget.TextView; 

public class MultiTouchGame extends Activity implements OnTouchListener { 
    StringBuilder builder = new StringBuilder(); 
    TextView textView; 

    boolean[] joystickTouched = new boolean[10]; 
    boolean[] fireButtonTouched = new boolean[10]; 
    int joyX = 0; 
    int joyY = 0; 

    int screenHeight; 
    int screenWidth; 
    Pad pad = new Pad(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     textView = new TextView(this); 
     textView.setText("Touch and drag (multiple fingers supported)!"); 
     textView.setOnTouchListener(this); 
     setContentView(textView); 

     WindowManager mWinMgr = (WindowManager)getSystemService(Context.WINDOW_SERVICE); 
     DisplayMetrics dm = new DisplayMetrics(); 
     mWinMgr.getDefaultDisplay().getMetrics(dm); 
     screenHeight = dm.heightPixels; 
     screenWidth = dm.widthPixels; 

    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     int action = event.getAction() & MotionEvent.ACTION_MASK; 
     int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; 
     int pI = event.getPointerId(pointerIndex); 
     switch (action) { 
      case MotionEvent.ACTION_DOWN: 
      case MotionEvent.ACTION_POINTER_DOWN: 
       joystickTouched[pI] = isPointerOnJoystick(event, pointerIndex); 
       fireButtonTouched[pI] = isPointerOnFireButton(event, pointerIndex); 
       break; 
      case MotionEvent.ACTION_UP:   
      case MotionEvent.ACTION_POINTER_UP: 
      case MotionEvent.ACTION_CANCEL: 
       joystickTouched[pI] = false; 
       fireButtonTouched[pI] = false; 
       break; 
      case MotionEvent.ACTION_MOVE: 
       joystickTouched[pI] = isPointerOnJoystick(event, pointerIndex); 
       fireButtonTouched[pI] = isPointerOnFireButton(event, pointerIndex); 
       if (isTouchingJoystick()){ 
        int index = getJoystickPointer(); 
        if (index != -1 && index < event.getPointerCount()){ 
         joyX = (int)event.getX(index); 
         joyY = (int)event.getY(index); 
        } 
       } 
       break; 
     } 

     updateTextView();  
     return true; 
    } 

    private boolean isPointerOnJoystick(MotionEvent event, int pointerIndex){ 
     return (event.getX(pointerIndex)<pad.getWidth()+(screenWidth*.18)&&event.getX(pointerIndex)>0&& 
       event.getY(pointerIndex)<(screenHeight)&&event.getY(pointerIndex)>(screenHeight-(0.42*screenHeight))); 
    } 

    private boolean isPointerOnFireButton(MotionEvent event, int pointerIndex){ 
     return (event.getX(pointerIndex)>screenWidth/3); 
    } 

    private boolean isTouchingJoystick(){ 
     for (int i = 0; i < joystickTouched.length; i++){ 
      if (joystickTouched[i]){ 
       return true; 
      } 
     } 
     return false; 
    } 

    private boolean isTouchingFireButton(){ 
     for (int i = 0; i < fireButtonTouched.length; i++){ 
      if (fireButtonTouched[i]){ 
       return true; 
      } 
     } 
     return false; 
    } 

    private int getJoystickPointer(){ 
     for (int i = 0; i < joystickTouched.length; i++){ 
      if (joystickTouched[i]){ 
       return i; 
      } 
     } 

     return -1; 
    } 

    private void updateTextView() { 
     builder.setLength(0); 
     builder.append(String.format("screenWidth[%d], screenHeight[%d]\n",screenWidth, screenHeight)); 
     builder.append(String.format("joystickTouched[%s]\n", isTouchingJoystick())); 
     builder.append(String.format("fireButtonTouched[%s]\n", isTouchingFireButton())); 
     builder.append(String.format("joystickMovement[%d,%d]",joyX,joyY)); 

     textView.setText(builder.toString()); 
    } 

    private static class Pad { 
     public int getWidth(){ 
     return 200; 
     } 
    } 

} 
関連する問題