[Android 2.2] Probleme with OnClickListener


Last Updated:

  1. Finalflo

    Finalflo New Member This Topic's Starter

    Joined:
    Oct 21, 2010
    Messages:
    2
    Likes Received:
    0
    Hy,

    I am currently programming a small game for Android like this one:

    http://www.jbmm.fr/mkportal/modules/gallery/album/a_1654.png

    So it is very simple. When I tap on the mole, I want the mole to be moved somewhere else. That's working fine.

    But now I added in the OnClick function the fact that I want the score to be printed in a label.

    My problem is that now it only print the score in the label and it's not moving the mole anymore.

    Here is my source code.

    Code (Text):
    1. package com.google.hit;
    2.  
    3. import java.util.Random;
    4.  
    5. import java.lang.annotation.Annotation;
    6. import android.app.Activity;
    7. import android.os.Bundle;
    8. import android.view.View;
    9. import android.widget.Button;
    10. import android.widget.TextView;
    11. import android.widget.Toast;
    12.  
    13. public class Jeux extends Activity implements View.OnClickListener{
    14.  
    15.     Button buttonImg2;
    16.     int position [][] = new int [13][2];
    17.     int score = 0;
    18.  
    19.     @Override
    20.     public void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.jeux);
    23.        
    24.         setPosition();
    25.         buttonImg2 = (Button) findViewById(R.id.logo2);
    26.         buttonImg2.setOnClickListener(this);
    27.  
    28.     }
    29.            
    30.     void setPosition(){
    31.         position[0][0] = 25;
    32.         position[0][1] = 115;
    33.        
    34.         position[1][0] = 165;
    35.         position[1][1] = 115;
    36.        
    37.         position[2][0] = 260;
    38.         position[2][1] = 115;
    39.        
    40.         position[3][0] = 90;
    41.         position[3][1] = 140;
    42.        
    43.         position[4][0] = 15;
    44.         position[4][1] = 180;
    45.  
    46.         position[5][0] = 150;
    47.         position[5][1] = 175;
    48.        
    49.         position[6][0] = 235;
    50.         position[6][1] = 165;
    51.        
    52.         position[7][0] = 75;
    53.         position[7][1] = 245;
    54.        
    55.         position[8][0] = 255;
    56.         position[8][1] = 235;
    57.        
    58.         position[9][0] = 165;
    59.         position[9][1] = 270;
    60.        
    61.         position[10][0] = 35;
    62.         position[10][1] = 315;
    63.        
    64.         position[11][0] = 123;
    65.         position[11][1] = 360;
    66.        
    67.         position[12][0] = 245;
    68.         position[12][1] = 330;
    69.     }
    70.    
    71.     public void onClick(View v) {
    72.         score++;
    73.         Toast.makeText(Jeux.this, String.valueOf(score), Toast.LENGTH_SHORT).show();
    74.         moveAndroid();
    75.     }
    76.    
    77.     void moveAndroid()
    78.     {
    79.         Random r = new Random();
    80.         int nb = r.nextInt(12);
    81.         buttonImg2.layout(position[nb][0],position[nb][1],40 +position[nb][0] ,46+position[nb][1]);
    82.         changeScore(score);
    83.     }
    84.    
    85.     void changeScore(int score2){
    86.         String test = String.valueOf("Score : " + String.valueOf(score2));
    87.         ((TextView) findViewById(R.id.textscore)).setText(test);
    88.     }
    89.  
    90. }
     

    Advertisement
  2. Finalflo

    Finalflo New Member This Topic's Starter

    Joined:
    Oct 21, 2010
    Messages:
    2
    Likes Received:
    0
    Maybe the problem is due to the function

    buttonImg2.layout(position[nb][0],position[nb][1],40 +position[nb][0] ,46+position[nb][1]);

    So if you have an other way to move the button can you tell me.

    Thank you !
     
  3. RNekic

    RNekic Member

    Joined:
    May 25, 2010
    Messages:
    12
    Likes Received:
    1
    This doesn't address your question but...FYI, according to the Random documentation, r.nextInt(12) includes 0 but does not include 12; so based on your position data, you should be using r.nextInt(13).

    "Returns a new pseudo-random int value which is uniformly distributed between 0 (inclusively) and the value of n (exclusively)."

    Also, there's no need to pass score to "changeScore".
     

Share This Page

Loading...