Sign in to follow this  
GameDeveloper93

OnTouch event android development issue

Recommended Posts

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

Edited by GameDeveloper93

Share this post


Link to post
Share on other sites

<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.

Share this post


Link to post
Share on other sites

<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 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this