2011-04-03 75 views
1

我正在开发一个游戏,其中不同的位图在屏幕上运行(SurfaceView)。由于速度不同,有时会出现两个字符叠加在一起的情况。我使用了OnTouch功能来捕捉玩家触摸角色的时刻。然而,当两个角色叠加时,我总是“触摸”后面的那个角色,我从来不碰他面前的另一个角色。为什么?我如何改变这种行为?由于我碰到前面的人,所以我总是碰到后面的人,真的很烦人。我希望问题很清楚。OnTouch位图覆盖

谢谢!

编辑:这是我的onTouchEvent功能:

@Override 
public boolean onTouchEvent(MotionEvent event){ 
    synchronized (getHolder()) { 

     boolean chibi_toccato = false; 

     if (!blocco_tocco){ 

//这里我有含有chibis结构(载体)中显示

 for (int i = (mChibiVector.size() - 1); i >= 0; i--) { 
      Chibi chibi = mChibiVector.elementAt(i); 


     if (event.getAction() == MotionEvent.ACTION_DOWN) {      

      if (!chibi_toccato){  

//触摸是一个标志,该标志告诉如果一个赤壁之前被触摸

   if (chibi.touched) { 

//如果我碰上船位图(scre上某处EN)的赤壁将是安全的

    int navexend = posiz_nave_x + thread.mNave.getWidth(); 
        int naveyend = posiz_nave_y + thread.mNave.getHeight(); 
        if ((int) event.getX() >= posiz_nave_x && 
          (int) event.getX() <= navexend && 
          (int) event.getY() >= posiz_nave_y && 
          (int) event.getY() <= naveyend){ 
         chibi.chibisalvo = true; 


         thread.calcola_vel_x(chibi); 
         thread.calcola_vel_y(chibi); 
         } else { 
          chibi.touched = false; 

          int temp_chibi_y = chibi.getCoordinate().getY(); 

          chibi.getCoordinate().setY(
            temp_chibi_y + 
            chibi.getBitmapDimensions()[1]); 

         } 
        chibi_toccato = true; 
       } else { 
       // Here I check if a chibi is touched: 

        chibi = thread.checkTouched(chibi, (int) event.getX(), 
          (int) event.getY()); 

        if (chibi.touched){ 
// If the chibi is touched, I save the X-Y info: 

         chibi.getCoordinate().setTouchedX((int) event.getX()); 
         chibi.getCoordinate().setTouchedY((int) event.getY()); 


         int temp_chibi_y = chibi.getCoordinate().getY(); 

         chibi.getCoordinate().setY(
           temp_chibi_y - 
           chibi.getBitmapDimensions()[1]); 

         chibi_toccato = true; 
         }      
        } 
       } 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 

       if (chibi.touched){ 

        chibi_toccato = false; 

       int navexend = posiz_nave_x + thread.mNave.getWidth(); 
       int naveyend = posiz_nave_y + thread.mNave.getHeight(); 
       if ((int) event.getX() >= posiz_nave_x && 
         (int) event.getX() <= navexend && 
         (int) event.getY() >= posiz_nave_y && 
         (int) event.getY() <= naveyend){ 
        chibi.chibisalvo = true; 

        thread.calcola_vel_x(chibi); 
        thread.calcola_vel_y(chibi); 
        } 
       } 
      } 
     } 
     }  
    }  
    return true; 
} 
+0

请提供您确定正在触摸的角色的代码片段。很明显,错误在那里,没有看到它很难猜出答案。 – 2011-04-03 17:34:30

回答

0

那么,过了一段时间,最后我得到了问题。该问题是在对于周期:

的for(int i =(mChibiVector.size() - 1); I> = 0;我 - )

自从我挑选从端帧的矢量,第一次触摸总是在其他人的背后。我仍然想知道为什么,但最后我改变了从第一个矢量项目到结束的循环,并且它工作。

希望它对其他人有用。