0

I am making a game app that displays random circles all across the screen. This is all done in the onDraw method by making a never ending loop. However in the onTouchEvent method there is code that is called when a circle is clicked. The problem is when a circle is "touched" nothing happens, but sometimes something does(if you click it a lot before it disappears). Id like to know if there is a way to get the onTouch method working so these circles can be clicked.

public class DrawingView extends View{



    public DrawingView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }
    RectF rectf = new RectF(0, 0, 200, 0);

    private static final int w = 100;
    public static int lastColor = Color.BLACK;
    private final Random random = new Random();
    private final Paint paint = new Paint();
    private final int radius = 230;
    private final Handler handler = new Handler();
    public static int redColor = Color.RED;
    public static int greenColor = Color.GREEN;
    int randomWidth = 0;
    int randomHeight = 0;
    public static int addPoints = 0;
    public static int savedScore;
    public static List<String> a = new ArrayList<String>();
    public static String[] savedScores = new String[a.size()];
    Paint red;
    public static int howManyPoints;
    public static int highestScore = 0;
    boolean isTouched;
    Thread newThread = new Thread();
    int t = 1;
    int l = 0;


    @Override 
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        //handler.post(updateCircle);

    }

    @Override 
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
       // handler.removeCallbacks(updateCircle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        // your other stuff here
        Paint back = new Paint();
        back.setColor(Color.BLACK);
        Rect background = new Rect();
        background.set(0, 0, canvas.getWidth(),canvas.getHeight() );
        canvas.drawRect(background, back);

        Paint newPaint = new Paint();
        newPaint.setColor(Color.BLUE);
        newPaint.setTextSize(60);
        canvas.drawText("Beta v2", 10, 60, newPaint);

        if(l < t){
            lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
            paint.setColor(lastColor);
            if(random == null){
                randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
                randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
            }else {
                randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
                randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
            }
            canvas.drawCircle(randomWidth , randomHeight , radius , paint);
            try {
                newThread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            invalidate();
        }


        red = new Paint();
        red.setColor(Color.BLUE);
        red.setTextSize(150);
        canvas.drawText("" + addPoints, 500, 1350, red);


    }




    @SuppressWarnings("deprecation")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            int x = (int) event.getX();
            int y = (int) event.getY();
                if(isInsideCircle(x, y) ==  true){
                        if(lastColor == redColor){
                            howManyPoints = addPoints;
                            if(howManyPoints > highestScore){
                                highestScore = howManyPoints;
                            }
                            //handler.removeCallbacks(updateCircle);
                            lastColor = redColor;
                            addPoints = 0;
                            Intent i = new Intent(this.getContext(), YouFailed.class);
                            this.getContext().startActivity(i);
                            l = 1;
                        } 
                        if(lastColor == greenColor){
                            addPoints++;
                            isTouched = true;
                            l = 0;

                        }
                }else  {

                }

        }
        return false;

}


public boolean isInsideCircle(int x, int y){
  if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius))){
    return true;
  }
  return false;    
}

}
Paul Trueman
  • 149
  • 11

1 Answers1

0

You forgot to return true when you touch down

Try to add this

if(event.getAction() == MotionEvent.ACTION_DOWN){
    //your code
    return true;
    }
Jafaska
  • 26
  • 5
  • so i add a return true above the return false? – Paul Trueman May 09 '15 at 16:08
  • You return false in your onTouchEvent method but return true inside if(event.getAction() == MotionEvent.ACTION_DOWN){ } – Jafaska May 09 '15 at 16:13
  • thats not the problem – Paul Trueman May 09 '15 at 16:50
  • In your code I see that you spawn a new circle every second, so I assume there will be multiple circles on the screen. In isInsideCircle, the value of randomWidth and randomHeight is the positition of the latest circle but the one you're touching might not be the latest one. Maybe that's the reason for your problem. – Jafaska May 09 '15 at 17:15
  • multiple circles will not be on the screen at the same time. it redraws every second – Paul Trueman May 11 '15 at 03:17