0

我在游戏中遇到以下问题:

假设我在 lvl 3 上有 5 个屏幕。每个屏幕都有自己的类。所以我有 5 个班级,名为 lvl 3_1_0、3_2_0 .... .... 3_5_0。我还有一个手势监听器,所以当你向右滑动时,它会转到右侧的屏幕。正常玩时一切正常,但如果用户开始疯狂地向右滑动,并且他每秒刷了很多次,游戏就会崩溃。

我想过添加一个“Thread.sleep”,但是当我让线程睡眠时我无法让它工作,加载类时游戏崩溃。我想我把它放在了错误的地方,或地方....如果手势监听器等待一秒钟才能激活,最好的办法是。我还有一个手势过滤器类。我不知道是否应该将 thread.sleep 放入手势过滤器类或每个类中的手势侦听器中。

这是 on create 和手势监听器的代码...

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;

          } }}

希望这可以帮助 !感谢 AndroidPenguin 的帮助 :)

我正在考虑的另一个解决方案有点滞后,而不是这个好是在手势过滤器类中制作一个 Runnable 来作为一个计时器,每次计时器达到例如 1 秒时,它都会返回“isoktodotheswipe”布尔值,并且每次擦拭了“isoktodotheswipe”布尔值将变为错误......

4

1 回答 1

1

设置一个布尔值

例如

boolean finished = false;

然后在您的手势/调度侦听器中,将以下内容添加到代码的最顶部:

if(!finished){
return;}

一旦你的班级完成设置。设置完成为真。这就是我解决同样问题的方法。

于 2012-12-15T03:21:38.067 回答