I am trying to implement pong Game, I have two classes as "PongGameView" and "GameState"
i implemented multitouch event implemented in "PongGameView" and passing the x cooridantes of the pads to "GameState".
i wanted to achieve that pads should move independently when user touches bottom pad, only bottom pad should move, if user touches , top pad, only top pad should move. But i am having the problem that both pads are moving if i try to move one of the pad. how would i achieve this?
public class PongGameView extends SurfaceView implements SurfaceHolder.Callback{
private GameThread _thread;
public PongGameView(Context context, AttributeSet attrs) {
super(context, attrs);
//So we can listen for events...
SurfaceHolder holder = getHolder();
holder.addCallback(this);
setFocusable(true);
//and instantiate the thread
_thread = new GameThread(holder, context, new Handler());
}
private float _x = 0;
private float _y = 0;
private int mActivePointerId;
public boolean onTouchEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
// Get the index of the pointer associated with the action.
int index = MotionEventCompat.getActionIndex(event);
int xPos = -1;
int yPos = -1;
boolean flag=false;
if (event.getPointerCount() > 1) {
// The coordinates of the current screen contact, relative to
// the responding View or Activity.
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
flag=true;
} else {
// Single touch event
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
}
_thread.getGameState().surfaceTouched(xPos, yPos, flag );
return true;
}
}
my second class is
public class GameState {
//the draw method
public void draw(Canvas canvas, Paint paint) {
//Clear the screen
canvas.drawRGB(20, 20, 20);
//set the colour
paint.setARGB(200, 0, 200, 0);
//draw the ball
canvas.drawRect(new Rect(_ballX,_ballY,_ballX + _ballSize,_ballY + _ballSize), paint);
paint.setARGB(200, 200,0, 0);
//draw the bats
canvas.drawRect(new Rect(_topBatX, _topBatY, _topBatX + _batLength, _topBatY + _batHeight), paint); //top bat
paint.setARGB(200,0,0,200);
canvas.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX + _batLength,
_bottomBatY + _batHeight), paint); //bottom bat
}
public boolean surfaceTouched(float posX, float posY, boolean flag) {
if (flag= true)
{
_bottomBatX = (int) posX;
}
else
{
_topBatX = (int) posX;
}
return true;
}