【问题标题】:OnTouch bitmap overlayedOnTouch 位图叠加
【发布时间】:2011-07-28 17:01:58
【问题描述】:

我正在开发一款在屏幕上运行不同位图的游戏(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;
}

【问题讨论】:

  • 请提供您确定被触摸字符的代码。显然错误就在那里,没有看到它很难猜出答案。

标签: android bitmap touch-event


【解决方案1】:

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

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

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

希望对其他人有用。

【讨论】:

    猜你喜欢
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多