0

I have this code where there is a Backgroung under an Hand, both a Bitmap images. Now, i have to control with an onClick method if the click touches the hand or not. For this, i wrote here and some guys tell me to use a method called "bitmap.getPixel(int x, int y)" that gives the color of pixel touched. For control if the click doesn't touch the hand, i write "bitmap.getPixel(x, y) != Color.TRASPARENT", but doesn't work perfectly. Under this i write the code of SurfaceView.

P.S. The bitmap "Hand" is a picture of a central Hand with parts trasparent around.

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{

private long missileStartTime;
private ThreadLv1 thread;
private OggettiLv1 hand;
private int conto=0;
private Sfondo sfondo;
MediaPlayer ouch;


public GamePanel(Context context){
    super(context);
    getHolder().addCallback(this);
    thread = new ThreadLv1(getHolder(), this);
    setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
    boolean retry = true;
    while(retry)
    {
        try{thread.setRunning(false);
            thread.join();

        }catch(InterruptedException e){e.printStackTrace();}
        retry = false;
    }
}


@Override
public void surfaceCreated(SurfaceHolder holder){
    hand = new OggettiLv1(BitmapFactory.decodeResource(getResources(), R.drawable.mano5));
    sfondo = new Sfondo(BitmapFactory.decodeResource(getResources(), R.drawable.sfondo));
    ouch= MediaPlayer.create(getContext(), R.raw.ouch);
    knifesong.start();
    thread.setRunning(true);
    thread.start();
}


public void update() {
}

@Override
public void draw(Canvas canvas)
{
    if(canvas!=null) {
        final int savedState = canvas.save();
        background.draw(canvas);
        hand.draw(canvas);

        drawText(canvas);
        canvas.restoreToCount(savedState);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    float x = event.getX();
    float y = event.getY();
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            int c = hand.sprite.getPixel((int) event.getX(), (int) event.getY());
            if(c!=Color.TRANSPARENT){
                conto++;
            }
            return true;
    }
    return false;
}

}

Someone know the problem and the solution in this code? Thanks in advance.

LolloAAA
  • 112
  • 10

1 Answers1

0

I think this might be the problem:

int c = hand.sprite.getPixel((int) event.getX(), (int) event.getY());

I think you are getting the value of a pixel at the x,y location on the sprite, you should get the value of the pixel on canvas.

EDIT:

Since your canvas is static, I suggest doing the following: add a Bitmap to your class

private Bitmap canvasState;

In the draw function, initialize canvasState before anything else

canvasState = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.RGB_565));
canvas.setBitmap(canvasState);

after that, every change that you make to the canvas will be made to the canvasState, and you can use that in your event handler like so:

int c = canvasState.getPixel((int) event.getX(), (int) event.getY());

I'm not sure if the code I wrote will compile (I wrote them from the top of my head) but I think you can work out compile errors yourself.

Tawcharowsky
  • 615
  • 4
  • 18