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;
}
請提供您確定正在觸摸的角色的代碼片段。很明顯,錯誤在那裏,沒有看到它很難猜出答案。 – 2011-04-03 17:34:30