Je fais une application qui affiche des cercles au hasard sur l'écran. Les cercles sont rouges ou verts. Le but de l'application est de faire en sorte que lorsque quelqu'un touche un cercle vert, il se passe quelque chose de bien, comme s'ils obtenaient des points ajoutés à leur score. Quand un cercle rouge est cliqué quelque chose de mal arrive comme une nouvelle activité est commencée et la page indique que vous avez échoué ou quelque chose. Voici mon code pour cette application. Dans ce code, je ne reçois aucune erreur, rien dans logcat, tout fonctionne très bien. Les cercles s'affichent au hasard sur l'écran ainsi que le score qui est par défaut 0. Le problème que j'ai avec cette application est que quand un cercle rouge ou vert est cliqué rien ne se passe.Lorsque quelqu'un touche un cercle rouge sur mon application rien ne se passe
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;
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
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);
paint.setColor(Color.BLACK);
paint.setTextSize(150);
canvas.drawText("Score: " + addPoints, 120, 300, paint);
}
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y) == true){
//Do your things here
if(redColor == lastColor){
Intent i = new Intent(v.getContext(), YouFailed.class);
v.getContext().startActivity(i);
} else {
addPoints++;
}
}else {
}
return true;
}
public boolean isInsideCircle(int x, int y){
if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius))){
return true;
}
return false;
}
}
Etes-vous sûr que votre contact (x, y) sont cercle à l'intérieur? Et pourquoi ne vérifiez-vous pas quelle est l'action, c'est-à-dire 'DOWN', 'MOVE' ou 'UP'. @Paul – Pooja
mettre le point de débogage ici "int x = (int) event.getX();" et assurez-vous qu'il est exécuté –