Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
}
share|improve this question
2  
Hi, Zahir, welcome to StackExchange! We're really about generating eternal Q&A archives of ways to accomplish things which can be useful to everyone, not providing individual code debugging assistance. This question probably would work better on a discussion forum, or in our chat. Our FAQ lists a bunch of spots which are set up to provide help with exactly this type of question. Good luck! :) – Trevor Powell Feb 20 at 0:49
That's no reason to refuse to give the man an answer, also his question isn't really all that specific, he gives a code example and says this is what happened, what needs to be done to get this... I think answering this question will indeed benefit someone out there as well as Zahir Abas. However I probably don't know best, and I am not about to get into a heated discussion, just a little statement... Thanks for listening. – Luke San Antonio Feb 20 at 2:40

closed as too localized by Byte56, Trevor Powell, Josh Petrie, Sean Middleditch, bummzack Feb 20 at 7:52

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.