Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Well, I'm obviously very new to game dev and android. I've got a ball bouncing around (successfully detecting screen edge collision 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());
        }
        //MOVE GESTURES
        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() );
                }

            }
        }
        //PRESS RELEASED
        if (e.getAction() == MotionEvent.ACTION_UP){
            if (isTouched()){
                //no longer being dragged
                setTouched(false);
            }
        }
    }

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; )
speed.toggleXDirection();

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.

If you want to download and view the source for the whole Paddle class you can find it here http://db.tt/MTiV076B and if needed, the Speed class here http://db.tt/Zz1z0veW

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
1  
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)' –  David Gouveia Dec 13 '11 at 15:20
    
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. –  Holly Dec 13 '11 at 15:54
    
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. ;-) –  David Gouveia Dec 14 '11 at 4:03
add comment

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.

share|improve this answer
    
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 :) –  Holly Dec 13 '11 at 15:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.