Добрый день, подскажите пожалуйста, может кто знает.
Пытаюсь сделать игру на андроид.
Суть: со старта появляется 2 шара, их необходимо объеденить, после чего они должны пропасть с экрана и сформироватся новая пачка шаров.
не могу понять где натупил, если убрать {} - возле invalidate(); - перерисовки экрана. Будут хаотично пересоздаваться шары....
хз де натупил
package com.example.mygame;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import androidx.appcompat.app.AppCompatActivity;
public class OneLevel extends AppCompatActivity {
float x;
float y;
// static int showDelay = 1000;
static int counter = 2;
static ArrayList<Figure> figures = new ArrayList<Figure>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawView(this));
}
class DrawView extends View {
Paint p;
Rect rect;
public DrawView(Context context) {
super(context);
p = new Paint();
rect = new Rect();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawARGB(80, 102, 204, 255);
if (figures.size()==0) {
genereteRandomDrawCircle(canvas, counter);
}
else {
invalidate();
Paint p = new Paint();
p.setColor(Color.RED);
for(Figure figure : figures){
canvas.drawCircle((float) figure.x,(float)figure.y,figure.d,p);
}
}
}
}
class Figure {
double x;
double y;
int d;
int color;
public Figure(double x, double y, int d, int color) {
this.x = x;
this.y = y;
this.d = d;
this.color = color;
}
}
public void genereteRandomDrawCircle(Canvas canvas, int count) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Paint p = new Paint();
p.setColor(Color.RED);
for (int i = 0; i < count; i++) {
Figure figure = new Figure(Math.random() * width, Math.random() * height, 50, Color.RED);
canvas.drawCircle((float) figure.x, (float) figure.y, (float) figure.d, p);
figures.add(figure);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
Figure figureTake = null; //получение фигуры по клику
float firstClickX; // x координата нажатия начальное положение пальца
float firstClickY; // у координата нажатия начальное положение пальца
int index = 0;
float lastClickX;
float lastClickY;
int index2 = 0;
Figure figurePush = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Figure foundСircle = new Figure(dx,dy,dr,color);
for (Figure figure : figures) {
if ((Math.pow((figure.x - x), 2) + Math.pow((figure.y - y), 2)) <= Math.pow(figure.d, 2)) {
firstClickX = x;
firstClickY = y;
figureTake = figure;
break;
} else {
index++;
}
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
for (Figure figure : figures) {
if ((Math.pow((figure.x - x), 2) + Math.pow((figure.y - y), 2)) <= Math.pow(figure.d, 2)) {
lastClickX = x;
lastClickY = y;
figurePush = figure;
break;
} else {
index2++;
}
}
case MotionEvent.ACTION_CANCEL:
// System.out.println(x + ' ' + y);
break;
}
if (figurePush!=null &&figureTake!=null){
checkDellete(figureTake,index,figurePush,index2);
}
return true;
}
public boolean checkDellete(Figure figure, int indexFgure1,Figure figure2, int indexFigure2) {
figures.remove(indexFgure1);
figures.remove(indexFigure2);
// new DrawView(this);
return true;
}
}