我正在开发一款在屏幕上运行不同位图的游戏(SurfaceView)。由于它们具有不同的速度,有时会发生两个字符叠加的情况,一个在另一个后面。我使用 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){
//touched 是一个标志,告诉 chibi 之前是否被触摸过
if (chibi.touched) {
//如果我触摸船位图(屏幕上的某处),赤壁将是安全的
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;
}
最佳答案
好吧,过了一会儿,我终于明白了。问题出在 for 循环中:
for (int i = (mChibiVector.size() - 1); i >= 0; i--)
自从我开始从矢量的末尾挑选帧,第一次触摸总是在其他图像后面。我仍然想知道为什么,但最后我更改了从第一个向量项到最后的 for 循环并且它起作用了。
希望对其他人有用。
关于android - OnTouch 位图覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5531002/