2015-04-20 2 views
0

Je fais une application Android. L'objectif est de faire des cercles que lorsque touché quelque chose se passe.En plus de détails, cette application affichera 2 types de cercles soit un rouge ou un vert. La couleur du cercle est stockée dans la variable lastColor. Le but de la couleur est de faire en sorte que lorsqu'un utilisateur "touche" un cercle rouge ou vert, il se passe quelque chose. Par exemple, si un cercle vert est touché, un point est ajouté à ce score, ou si un cercle rouge est cliqué, l'activité est modifiée, mais lorsque l'un des cercles est cliqué rien ne se passe. Voici la méthode onTouchMa méthode onTouch ne fait pas ce qu'elle est programmée

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; 
} 

Cette partie du code est la chose qui ne fonctionne pas correctement. Voici la classe entière ci-dessous.

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 = 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/2f, 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; 
    } 
} 
+4

* "lorsque vous touchez quelque chose se produit." * Comme quoi? * "Cette partie du code est la chose qui ne fonctionne pas correctement" * comment? – codeMagic

+0

quand un cercle vert ou rouge est "touché" il est supposé faire quelque chose. si c'est un cercle rouge il affichera une autre activité si c'est un cercle vert il ajoutera un point, mais bien sûr il ne se passe rien quand je clique n'importe où –

+0

Est-ce qu'il plante ou simplement ne fait rien? Avez-vous mis des journaux et/ou des points d'arrêt pour voir ce qui est/n'est pas appelé? En l'état actuel des choses, il s'agit d'une question assez vaste avec un manque d'auto-débogage qui rend difficile la résolution d'autres problèmes. – codeMagic

Répondre

0

Vous devez remplacer la fonction onTouchEvent. J'ai testé le code et ça marche! Assurez-vous également de renvoyer false après addPoints ++ pour vous assurer qu'un seul point est ajouté. J'espère que cela fonctionne pour vous!

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    int x = (int) event.getX(); 
    int y = (int) event.getY(); 
    if (isInsideCircle(x, y)) { 
     //Do your things here 
     if (redColor == lastColor) { 
      Intent i = new Intent(getContext(), null); 
      getContext().startActivity(i); 
     } 
     else { 
      addPoints++; 
      return false; 
     } 
    } 
    else { 

    } 
    return true; 
}