Apps [Android 2.2] Probleme with OnClickListener

Finalflo

Lurker
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:
package com.google.hit;

import java.util.Random;

import java.lang.annotation.Annotation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Jeux extends Activity implements View.OnClickListener{

	Button buttonImg2;
	int position [][] = new int [13][2];
	int score = 0;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jeux);
        
        setPosition();
        buttonImg2 = (Button) findViewById(R.id.logo2);
        buttonImg2.setOnClickListener(this);
  
    }
        	
    void setPosition(){
    	position[0][0] = 25;
        position[0][1] = 115;
        
        position[1][0] = 165;
        position[1][1] = 115;
        
        position[2][0] = 260;
        position[2][1] = 115;
        
        position[3][0] = 90;
        position[3][1] = 140;
        
        position[4][0] = 15;
        position[4][1] = 180;

        position[5][0] = 150;
        position[5][1] = 175;
        
        position[6][0] = 235;
        position[6][1] = 165;
        
        position[7][0] = 75;
        position[7][1] = 245;
        
        position[8][0] = 255;
        position[8][1] = 235;
        
        position[9][0] = 165;
        position[9][1] = 270;
        
        position[10][0] = 35;
        position[10][1] = 315;
        
        position[11][0] = 123;
        position[11][1] = 360;
        
        position[12][0] = 245;
        position[12][1] = 330;
    }
    
	public void onClick(View v) {
		score++;
		Toast.makeText(Jeux.this, String.valueOf(score), Toast.LENGTH_SHORT).show();
		moveAndroid();
	}
	
	void moveAndroid()
	{
		Random r = new Random();
		int nb = r.nextInt(12);
		buttonImg2.layout(position[nb][0],position[nb][1],40 +position[nb][0] ,46+position[nb][1]);
		changeScore(score);
	}
	
    void changeScore(int score2){
    	String test = String.valueOf("Score : " + String.valueOf(score2));
    	((TextView) findViewById(R.id.textscore)).setText(test);
    }

}
 

Finalflo

Lurker
Thread starter
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 !
 

RNekic

Newbie
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".
 
Top