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());
}
//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.