OnTouch event android development issue

Started by
3 comments, last by GameDeveloper93 9 years, 2 months ago

I'm building a touch game where you have to touch the inside of an image that was drawn on a canvas. The image gets drawn at new positions every few secs anyways the issue is detecting a touch inside the image. The below is my touch event and it does work however it only detects the touch if you touch the top of the image but anywhere else it fails to




    synchronized public boolean onTouchEvent(MotionEvent event){
        boolean handled = false;

        int action = event.getAction();

        int x = (int) event.getRawX(); //or event.getRawX();
        int y = (int) event.getRawY();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                touchDownTime = SystemClock.elapsedRealtime();
                break;

            case MotionEvent.ACTION_UP:

                if ( x >= bonX && x < bonX + ((GameWorld)findViewById(R.id.game_canvas)).joviWidth()
                     && y >= bonY && y < bonY + ((GameWorld)findViewById(R.id.game_canvas)).joviHeight() ) {

                   Log.d("TOuched",x+","+y);
                }

                    handled = true;

                break;
        }

        return handled;
    }

how you guys will be able to provide some help thank you smile.png

Edit: Just notice I may of posted this in the wrong area i'm sorry if i did

Advertisement

<moderator> Moved to Mobile & Console development forum </moderator>

MotionEvent.getRawX() and getRawY() return the location in screen coordinates, not local view coordinates. You should most likely be using MotionEvent.getX() and getY() instead.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

<moderator> Moved to Mobile & Console development forum </moderator>

MotionEvent.getRawX() and getRawY() return the location in screen coordinates, not local view coordinates. You should most likely be using MotionEvent.getX() and getY() instead.

For got to mention i have tried both getX() and getY() and also getRawX() and getRawY() both have gave me the same result unfortunately

Have you attempted to print out the x and y values of all touches, to see what values they have in various locations?

My next hunch would be that the Y axis runs the opposite way than you expect.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Have you attempted to print out the x and y values of all touches, to see what values they have in various locations?

My next hunch would be that the Y axis runs the opposite way than you expect.

Yes the x and y seems to be correct do u have anymore ideas what may be causing this?

This topic is closed to new replies.

Advertisement