ゲームで次の問題が発生しています。
レベル3に5つの画面があるとします。各画面にはそれ自体のクラスがあります。だから私はlvl3_1_0、3_2_0 ........3_5_0という名前の5つのクラスを持っています。ジェスチャーリスナーもあるので、右にスワイプすると右側の画面に移動します。通常のプレイではすべて正常に動作しますが、たとえばユーザーが狂ったように右にスワイプし始め、1秒間に何度もスワイプすると、ゲームはコースから外れてクラッシュします。
「Thread.sleep」を追加することを考えましたが、スレッドをスリープ状態にすると、クラスのロード時にゲームがクラッシュします。私はそれを間違った場所に置いていると思います....ジェスチャーリスナーがアクティブになるのを2番目に待つのが最善でしょう。ジェスチャーフィルタークラスもあります。thread.sleepをジェスチャフィルタークラスに入れるべきか、すべてのクラスにあるジェスチャリスナーに入れるべきかわかりません。
これがoncreateとジェスチャーリスナーのコードです...
public class lvl3_5_0 extends Activity implements View.OnTouchListener , SimpleGestureListener {
public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences settings;
SharedPreferences.Editor editor;
private SimpleGestureFilter detector;
static final int MIN_DISTANCE = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
detector = new SimpleGestureFilter(this,this);
super.onCreate(savedInstanceState);
setContentView(R.layout.lvl3_5_0);
ImageView iv = (ImageView) findViewById (R.id.image);
if (iv != null) {
iv.setOnTouchListener (this);
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
//-----------------------------
// Gesture detection
@Override
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT : {
{ Intent game = new Intent(lvl3_5_0.this, lvl3_4_0.class);
game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mHandler.removeCallbacks(dismissPopup);
this.finish(); startActivity(game);
}break;}
case SimpleGestureFilter.SWIPE_LEFT :{
{ Intent game = new Intent(lvl3_5_0.this, lvl3_1_0.class);
game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mHandler.removeCallbacks(dismissPopup);
this.finish(); startActivity(game);
}break;}
case SimpleGestureFilter.SWIPE_DOWN : {
break;}
case SimpleGestureFilter.SWIPE_UP :{
openOptionsMenu(); ;
}
break;
}
}
そして、これが私のジェスチャフィルタークラスです
public class SimpleGestureFilter extends SimpleOnGestureListener{
public final static int SWIPE_UP = 1;
public final static int SWIPE_DOWN = 2;
public final static int SWIPE_LEFT = 3;
public final static int SWIPE_RIGHT = 4;
public final static int MODE_TRANSPARENT = 0;
public final static int MODE_SOLID = 1;
public final static int MODE_DYNAMIC = 2;
private final static int ACTION_FAKE = -13; //just an unlikely number
private int swipe_Min_Distance = 65;
private int swipe_Max_Distance = 1111100;
private int swipe_Min_Velocity = 60;
private int mode = MODE_DYNAMIC;
private boolean running = true;
private boolean tapIndicator = false;
private Activity context;
private GestureDetector detector;
private SimpleGestureListener listener;
public SimpleGestureFilter(Activity context,SimpleGestureListener sgl) {
this.context = context;
this.detector = new GestureDetector(context, this);
this.listener = sgl;
}
public void onTouchEvent(MotionEvent event){
if(!this.running)
return;
boolean result = this.detector.onTouchEvent(event);
if(this.mode == MODE_SOLID)
event.setAction(MotionEvent.ACTION_CANCEL);
else if (this.mode == MODE_DYNAMIC) {
if(event.getAction() == ACTION_FAKE)
event.setAction(MotionEvent.ACTION_UP);
else if (result)
event.setAction(MotionEvent.ACTION_CANCEL);
else if(this.tapIndicator){
event.setAction(MotionEvent.ACTION_DOWN);
this.tapIndicator = false;
}
}
//else just do nothing, it's Transparent
}
public void setMode(int m){
this.mode = m;
}
public int getMode(){
return this.mode;
}
public void setEnabled(boolean status){
this.running = status;
}
public void setSwipeMaxDistance(int distance){
this.swipe_Max_Distance = distance;
}
public void setSwipeMinDistance(int distance){
this.swipe_Min_Distance = distance;
}
public void setSwipeMinVelocity(int distance){
this.swipe_Min_Velocity = distance;
}
public int getSwipeMaxDistance(){
return this.swipe_Max_Distance;
}
public int getSwipeMinDistance(){
return this.swipe_Min_Distance;
}
public int getSwipeMinVelocity(){
return this.swipe_Min_Velocity;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
final float xDistance = Math.abs(e1.getX() - e2.getX());
final float yDistance = Math.abs(e1.getY() - e2.getY());
if(xDistance > this.swipe_Max_Distance || yDistance > this.swipe_Max_Distance)
return false;
velocityX = Math.abs(velocityX);
velocityY = Math.abs(velocityY);
boolean result = false;
if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance){
if(e1.getX() > e2.getX()) // right to left
this.listener.onSwipe(SWIPE_LEFT);
else
this.listener.onSwipe(SWIPE_RIGHT);
result = true;
}
else if(velocityY > this.swipe_Min_Velocity && yDistance > this.swipe_Min_Distance){
if(e1.getY() > e2.getY()) // bottom to up
this.listener.onSwipe(SWIPE_UP);
else
this.listener.onSwipe(SWIPE_DOWN);
result = true;
}
return result;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
this.tapIndicator = true;
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent arg0) {
if(this.mode == MODE_DYNAMIC){ // we owe an ACTION_UP, so we fake an
arg0.setAction(ACTION_FAKE); //action which will be converted to an ACTION_UP later.
this.context.dispatchTouchEvent(arg0);
}
return false;
}
static interface SimpleGestureListener{
void onSwipe(int direction);
}
}
したがって、質問は次のようになります。「Thread.sleep(1000);」をどこに配置すればよいですか。try / catchに囲まれていますか?(それが私が使用しなければならないものである場合)
たくさん試してみましたが、行き詰まりました
----------------------------解決済み--------------------- -----------------
これが修正されたコードです
public class lvl3_3_0 extends Activity implements View.OnTouchListener , SimpleGestureListener {
boolean finished = false; // This was added ( no need to explain XD )
private SimpleGestureFilter detector;
static final int MIN_DISTANCE = 100;
// Handler and runnable reference
private PopupWindow popupWindow;
private Handler mHandler = new Handler();
private Runnable dismissPopup = new Runnable() {
public void run() {
if (popupWindow.isShowing())
popupWindow.dismiss();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {{
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
detector = new SimpleGestureFilter(this,this);
settings = getSharedPreferences(PREFS_NAME, 0);
editor = settings.edit();
editor.putString("currentscreen", "com.appsimple.roomdroid.lvl3_3_0");
editor.commit();
super.onCreate(savedInstanceState);
setContentView(R.layout.lvl3_3_0);
ImageView iv = (ImageView) findViewById (R.id.image);
if (iv != null) {
iv.setOnTouchListener (this);
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
finished = true; // This will make true the boolean after finishing the loading of the activity
}
//-----------------------------
// Gesture detection
@Override
public boolean dispatchTouchEvent(MotionEvent me){
if (!finished){} // This was added to make the gesturelistener not to listen if the activity has not finished loading.
else {
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me); }
return false;
}
@Override
public void onSwipe(int direction) {
if (!finished){}
else {
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT : {
finished = false; // this wass added after doing the swipe to avoid the gesture listener to have a que of events.
{ Intent game = new Intent(lvl3_3_0.this, lvl3_2_0.class);
game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mHandler.removeCallbacks(dismissPopup);
this.finish(); startActivity(game);
}break;}
case SimpleGestureFilter.SWIPE_LEFT :{
{ finished = false; Intent game = new Intent(lvl3_3_0.this, lvl3_4_0.class);
game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mHandler.removeCallbacks(dismissPopup);
this.finish(); startActivity(game);
}break;}
case SimpleGestureFilter.SWIPE_DOWN : {
break;}
case SimpleGestureFilter.SWIPE_UP :{
openOptionsMenu(); ;
}
break;
} }}
お役に立てれば !ThaksAndroidPenguinの助けを借りて:)
私が考えていた他の解決策は少し遅れでしたが、これはジェスチャーフィルタークラスのRunnableをタイマーとして機能させることでした。たとえば、タイマーが1秒になるたびに、「isoktodotheswipe」ブール値が返されます。ワイプが行われた「isoktodotheswipe」ブール値はfalseになります...