Reputation: 11
A few of my friends are collaborating together to develop an app for our high school computer science end of the year assignment. We are making an altered pong-type game. I am working on the Android version and two are working on the iOS version. Everything's been going well so far, the main language we use in class is Python so transitioning to Java has been a bit of a learning curve.
I am struggling with dragging the two paddles at once. I've read many post on here and on the Android developers page, and most of the code has come from there, but I can't quite get it. When one pointer is on the device both paddles drag well, but once the second pointer hits the screen it disregards the first pointer and only uses the second pointer, until it is released. Here's my code...
private final static int INVALID_POINTER_ID = -1;
private int mActivePointerId = INVALID_POINTER_ID;
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = MotionEventCompat.getActionMasked(ev);
switch (action) {
case MotionEvent.ACTION_DOWN: {
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
mLastTouchX1 = MotionEventCompat.getX(ev, pointerIndex);
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
break;
}
case MotionEvent.ACTION_POINTER_DOWN:{
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
if (pointerIndex == 1) {
mLastTouchX1 = MotionEventCompat.getX(ev, pointerIndex);
}
mActivePointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex =
MotionEventCompat.findPointerIndex(ev, mActivePointerId);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);
final float deltaX = x - mLastTouchX1;
paddle1.update(deltaX, screenX, y, screenY);
paddle2.update(deltaX, screenX, y, screenY);
mLastTouchX1 = x;
invalidate();
break;
}
case MotionEvent.ACTION_UP: {
break;
}
case MotionEvent.ACTION_CANCEL: {
break;
}
case MotionEvent.ACTION_POINTER_UP:{
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX2 = MotionEventCompat.getX(ev, newPointerIndex);
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
}
break;
}
}
Paddle1 and paddle2 are instances of my paddle class. Here is my update method in the class.
public void update ( float x, int screenX, float y, int screenY){
if (y > screenY / 2){
rect1.left = rect1.left + x;
rect1.right = rect1.left + length;
if (rect1.left < 0 ){
rect1.left=0;
rect1.right = rect1.left + length;
}
if (rect1.right > screenX){
rect1.left = screenX - length;
rect1.right = screenX;
}
}
else{
rect2.left = rect2.left + x;
rect2.right = rect2.left + length;
if (rect2.left < 0 ){
rect2.left=0;
rect2.right = rect2.left + length;
}
if (rect2.right > screenX){
rect2.left = screenX - length;
rect2.right = screenX;
}
}
I realize my code may be sloppy, but my end goal is to be able to move the two paddles simultaneously. If anyone can show me what I need to add or tweak to get there that would be great.
Thank you.
Edit: Using Android Studio.
Upvotes: 1
Views: 128
Reputation:
Searched The Web And Found A Good Article On Your Problem, vogella.com/tutorials/AndroidTouch/article.html Next Time However Do A Quick Search On Google Before You Ask Questions.Rember The Internet is a big place full of information ;)
Upvotes: 1