I have a circle that I move with an onTouch and a static rectangle. I want to identify when the circle hits the rectangle but I can not do it. Can someone tell me what I'm doing wrong?
Thank you.
Game Class:
public class Juego extends View implements View.OnTouchListener {
circulo bola = new circulo(this);
rectangulo cuadrado = new rectangulo(this);
public boolean checkCollision = false;
Paint paint;
public Juego(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
this.setOnTouchListener(this);
setFocusable(true);
paint = new Paint();
}
public void onDraw(Canvas canvas)
{
bola.paint(canvas);
cuadrado.paint(canvas);
}
private boolean collision() {
if (bola.getBounds().intersect(cuadrado.getBounds()))
checkCollision = true;
return checkCollision;
}
public boolean onTouch(View view, MotionEvent event)
{
circulo.x = (int)event.getX();
circulo.y = (int)event.getY();
if (collision()){
Toast.makeText(getContext(), "Colision", Toast.LENGTH_SHORT).show();
}
invalidate();
return true;
}
}
Rectangle class:
public class rectangulo{
private static final int Y = 200;
private static final int WITH = 400;
private static final int HEIGHT = 400;
int x = 200;
private Juego juego;
public rectangulo(Juego juego) {
this.juego = juego;
}
public void paint(Canvas canvas) {
Paint paint = new Paint();
canvas.drawRect(x, Y, WITH, HEIGHT, paint);
}
public Rect getBounds() {
return new Rect(x, Y, WITH, HEIGHT);
}
}
Circle class:
public class circulo{
private static final int DIAMETER = 50;
int x = 100;
int y = 100;
private Juego juego;
public circulo(Juego juego) {
this.juego = juego;
}
public void paint(Canvas canvas) {
Paint paint = new Paint();
canvas.drawCircle(x, y, DIAMETER, paint);
}
public Rect getBounds() {
return new Rect(x, y, DIAMETER, DIAMETER);
}
}