Zappescu
Zappescu

Reputation: 1439

OnTouch bitmap overlayed

I'm developing a game where different bitmap run on the screen (a SurfaceView). Since they have different speed, it happen that sometimes two characters are superimposed, one behind the other. I've used the OnTouch function to catch the moment when the player touch the character. However, when two characters are superimposed, I always "touch" the one that is behind, I never touch the other one in front of him. Why? How I can change this behaviour? Since I'm touching the one in front, it's is really annoying that I always touch the other behind. I hope the problem is clear.

Thanks!

Edit: This is my OnTouchEvent function:

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

        boolean chibi_toccato = false;

        if (!blocco_tocco){

// Here I have the structure (a Vector) containing the chibis to be shown

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


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

            if (!chibi_toccato){    

// touched is a flag that tell if a chibi were touched before

                if (chibi.touched) {

// if I touch on the boat bitmap (somewhere on the screen) the chibi will be safe

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

Upvotes: 1

Views: 415

Answers (1)

Zappescu
Zappescu

Reputation: 1439

Well, after a while, finally I got the issue. The problem were in the for cycle:

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

Since I started to pick the frames from the end of the vector, the first touch were always on the image behind the others. I'm still wondering why, but finally I changed the for cycle starting from the first vector item to the end and it worked.

Hope it could be useful for others.

Upvotes: 1

Related Questions