嗨,即時製作一個遊戲,將在屏幕上創建隨機圈。隨機創建的圓圈將具有紅色或綠色的值。我的問題是,我希望不僅能夠確定用戶何時點擊其中一個圈子,而是確定他們最終點擊了哪個圈子(紅色或綠色)。以下是我的代碼如下。我的主要問題是試圖找到將被吸引試圖獲得在屏幕上繪製的每個隨機圓的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