Well, I'm obviously very new to game dev and Android. I've got a ball bouncing around (successfully detecting screen edge collisions so that the bitmap stays within screen) and a paddle that the user can move left and right with their finger.

At the moment there's a few things wrong with my method, but I will post it as is:

(current method from my paddle class that I use to manage touchevents:)

public void onTouchEvents(MotionEvent e, int viewW, int viewH){
        //DETECT PRESS
        if (e.getAction() == MotionEvent.ACTION_DOWN){
            // delegating event handling to the paddle
            handleActionDown((int)e.getX(), (int)e.getY());
        if (e.getAction() == MotionEvent.ACTION_MOVE){
            if (isTouched()){
                //paddle is being dragged
                //Log.i(TAG, "paddle is being dragged");

                //SETTING NEW POSITION
                boolean collision = false; // to check wall collisions

                if (speed.getxDirection() == Speed.LEFT
                        && getX() - (bitmap.getWidth() /2) <= 0)
                {//Left wall collision
                    collision = true;
                    Log.i(TAG, "left wall col");

                if (speed.getxDirection() == Speed.RIGHT 
                        && getX() + (bitmap.getWidth() /2) >= viewW)
                {//right wall collision
                    collision = true;
                    Log.i(TAG, "right wall col");


                if(collision == false){ //No wall collisions, no problem
                    setX( (int)e.getX() );

        if (e.getAction() == MotionEvent.ACTION_UP){
            if (isTouched()){
                //no longer being dragged

At the moment when my paddle hits the right side of the screen it stops where it should (without the bitmap going off screen) but the user is unable to drag it back, presumably because collision is now stuck set at "false" so setX is never called again.

Oddly, when the paddle hits the left side of the screen the bitmap can just be continued to be dragged off screen and the collision is never detected.

Obviously I can recognize that there are two different issues here and I've had several ideas of how to solve the first, still can't figure out whats wrong with the second.

To solve the first I've tried getting rid of the collision variable all together. I've tried putting setting the x position back at various points

//eg. (after right wall collision detected)
setX( (int)e.getX() - 1);

or using my toggle x direction method, the same I got working with my ball object

//eg ( xDirection = xDirection * -1; )

I've tried all kinds of re-positioning of the code that I could think of that would possibly get it working as intended but I haven't been able to figure it out, any help would be appreciated.


2 Answers 2


There's no need to create a special flag about wall collisions. Do something like this (in pseudo-code):

newX = event.X;
if(newX < leftBound){
    newX = leftBound;
} else if(newX > rightBound){
    newX = rightBound;
paddle.x = newX;

So basically restrict the new X position to valid positions... that should do the trick.

  • 1
    \$\begingroup\$ I agree. Also, check the Java library to see if it has a method called "clamp" (I've seen one under MathUtils, dunno if that's standard) or just create your own, since it's pretty useful. It will allow you to do all of this in a single statement: 'paddle.x = clamp(paddle.x, leftBound, rightBound)' \$\endgroup\$ Commented Dec 13, 2011 at 15:20
  • \$\begingroup\$ Of course! i can't believe how much i was over complicating things. Its not a good trait is it? haha anyway thanks very much. Got it working very simply now. \$\endgroup\$
    – Holly
    Commented Dec 13, 2011 at 15:54
  • \$\begingroup\$ You learn these tricks as you go. My first game which was done in pascal was a single switch statement, five levels deep, with code repeated in every branch. I didnt even think about splitting it into functions. ;-) \$\endgroup\$ Commented Dec 14, 2011 at 4:03

There are many Java implementation of Pong available. They can give you ideas for solving most logic bugs. Here is a list of a few that I've found with source:

I also have versions from former students that include a fair number of features, including a mind bending panic mode. If you are really interested, I can ask them to post their versions.

  • \$\begingroup\$ Thanks for the links, will definitely have a look through, particularly next time i get stuck. I have a few ideas for other features but the "mindbending panic mode" has completely piqued my curiosity! Posting versions would be appreceiated but isn't necessary, only if its not any trouble :) \$\endgroup\$
    – Holly
    Commented Dec 13, 2011 at 15:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .