0

I want a score to add up to a score variable every time the user clicks on a bitmap. The score is then going to be used in another class. But so far i cant get it to work, i dont get any errors when running but it just always displays 0 (which is the default string in the Text View).

This is the method that counts the score and should "set" it in another script:

@Override public void run(){

            Point size = new Point();
            Display display = getWindowManager().getDefaultDisplay();
            display.getSize(size);
            Random wR = new Random();
            Random hR = new Random();
            int width = size.x;
            int height = size.y;
            int randomW = wR.nextInt(width-50)+50;
            int randomH = hR.nextInt(height-50)+50;

            CheckScore scoreClass = new CheckScore();

            while (running){

                if(!surface.getSurface().isValid())
                    continue;

                Canvas canvas = surface.lockCanvas();
                canvas.drawColor(Color.rgb(85, 107, 47));
                canvas.drawBitmap(incect[frame], randomW, randomH, null);

                if ((touch.x >= randomW && touch.x <(randomW + xBitmap) && (touch.y >= randomH && touch.y < (randomH+yBitmap)) && move == true)){
                    randomW = wR.nextInt(width-50)+50;
                    randomH = hR.nextInt(height-50)+50;
                    if (randomW > width - 200){
                        randomW = width - 200;
                    }
                    if (randomH > height - 200){
                        randomH = height - 200;
                    }
                    canvas.drawBitmap(incect[frame], randomW, randomH, null);
                    hasMoved = true;

                    scoreClass.score = score;
                    score = score +1;
                }

And this is the scripts that should recieve the score and display it:

    package com.example.bena.23;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class CheckScore extends AppCompatActivity {

    int score;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.score);
        getScore();
    }

    public void getScore(){
        TextView text = (TextView)findViewById(R.id.thisScore);
        text.setText(Integer.toString(score));
    }
}

I only wrote the method (run()) because of the script size. I would much appreciate if some one could help me in the matter.

Taegos
  • 333
  • 2
  • 5
  • 15

1 Answers1

0

You are calling the getScore() method only when the Activity is created. You should call the method after the user clicked the bitmap.

Also, you should not instance Activities like that. If you want to open an activity, use an intent (this may be an Activity from which you launch another):

startActivity(new Intent(this, CheckScore.class));

I suggest you create a Java class for your score, and store it in a static variable, change it when necessary in run() method. Then, let CheckScore activity retrieve the value of the variable when doing setText(). Correct me if I understood your idea incorrectly: the run() method and CheckScore are two different activities (cannot be shown at the same time)?

kit
  • 403
  • 1
  • 5
  • 17
  • Thank you for the answer, run () is a method in another activity and CheckScore is an activity. I only posted the run method because that script is very large. – Taegos Apr 11 '16 at 21:51