あなたが孤立して、ジョイスティックや火災のボタンを操作できることを考えると、それはあなたが正しく画面サイズを取得しているし、あなたのゲームオブジェクトへのポインタの周辺を検出に関する他のすべてが正しいことを理にかなっています。私は特定の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;
}
}
}
それは、ハードウェアのことだ場合は試してみてください。 Googleデバイスの中には、マルチタッチに奇妙な問題があるものがあります。https://play.google.com/store/apps/details?id=multitouchpro.tests&hl=de – devsnd
ハードウェアの問題ではないと思います。マルチタッチは私の電話でうまくいきます。私は銀河のs2でそれをテストしてきた – user1229888