嗨,即时制作一个游戏,将在屏幕上创建随机圈。随机创建的圆圈将具有红色或绿色的值。我的问题是,我希望不仅能够确定用户何时点击其中一个圈子,而是确定他们最终点击了哪个圈子(红色或绿色)。以下是我的代码如下。我的主要问题是试图找到将被吸引试图获得在屏幕上绘制的每个随机圆的x,y坐标
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 =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
int randomHeight = (random.nextInt((int) Math.abs((getHeight()-radius/2 + radius/2f))));
private final Runnable updateCircle = new Runnable() {
@Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
@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
canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
}
@Override
public boolean onTouchEvent (MotionEvent event) {
double r = Math.sqrt(((randomWidth^2)+(randomHeight^2)));
int maxX = (int) (((randomWidth)*(randomWidth)) + r);
int minX = (int) ((((randomWidth)*(randomWidth))) - r);
int maxY = (int) (((randomHeight)*(randomHeight)) + r);
int minY = (int) ((((randomHeight)*(randomHeight))) - r);
public int xCoordinateOfRedColor(){
if(redColor == lastColor){
if(randomWidth > maxX && < minX){
event.getX();
}
};
}
public int yCoordinateOfRedColor(){
if(redColor == lastColor){
if(randomHeight > maxY && < minY){
event.getX();
}
};
}
if(redColor == lastColor){
if(randomHeight > maxY && < minY){
event.getY();
}
};
if(greenColor == lastColor){
};
if(greenColor == lastColor){
};
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
randomWidth = (int) event.getX();
randomHeight = (int) event.getY();
invalidate();
break;
case MotionEvent.ACTION_POINTER_UP :
break;
}
return true;
}
}
我不知道如果IM接近或真正远离圆的x和y。
真的没有人能回答这个问题吗?好难过 – 2015-04-01 05:55:28
寒意。我在打字:P – CandiedOrange 2015-04-01 05:55:55
哈哈对不起,我刚才一直在这个问题上坐了很长时间,我有点慌乱。慢慢来。 – 2015-04-01 05:57:04