1

我正在开发一个在屏幕上运行不同位图的游戏(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 是一个标志,表明之前是否触摸过赤壁

                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;
}
4

1 回答 1

1

好吧,过了一会儿,我终于明白了这个问题。问题出在for循环中:

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

由于我开始从矢量末端挑选帧,所以第一次触摸总是在其他图像后面。我仍然想知道为什么,但最后我将 for 循环从第一个矢量项开始更改为结束并且它起作用了。

希望它对其他人有用。

于 2011-04-12T15:53:19.437 回答