0

I'm making simple pong game and I'm trying to make rectangle top stop moving left or right when it hits end of screen. I tried to do that with few Ifs in MotionEvent part, but its not working, it just gets off the screen like there arent any Ifs :D

I'm using this to get size of screen:

        Point size = new Point();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        display.getSize(size);
        canvasWidth = size.x;
        canvasHeight = size.y;

MotionEvent detection:

    public boolean doTouchEvent(MotionEvent event){


        synchronized(mSurfaceHolder){

            switch(event.getAction()){


                case MotionEvent.ACTION_MOVE:{
                    xTouch = (int)event.getX();
                    yTouch = (int)event.getY();

                    rectLeft = (float)xTouch-200;
                    rectTop = (float)yTouch+25;
                    rectRight = (float)xTouch+200;
                    rectBot = (float)yTouch-25;

                    if(rectLeft <= 0)
                        rectLeft = 0;
                    if(rectBot >= canvasWidth)
                        rectBot = canvasWidth;
                }
            }

        }
    }

And drawing rectangle:

public void draw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.draw(canvas);
    if(firstClick){
        //canvas.drawBitmap(img, xTouch - (imgWidth/2), yTouch - (imgHeight/2), null);

        canvas.drawRect(rectLeft, rectTop, rectRight, rectBot, paint);

    }
}
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
imot01
  • 97
  • 15

1 Answers1

0

In your code:

if(rectLeft <= 0)
                    rectLeft = 0;

I think your forgot about setting rectRight. Maybe try:

if(rectLeft <= 0) {
                    rectLeft = 0;
                    rectRight = 200;
}

or something like that.

donfuxx
  • 11,277
  • 6
  • 44
  • 76