jle
jle

Reputation: 696

android - creating a score count

I try to make a game and I need to count the score. The score is generated in the load() method and should be put at TextView score. The problem that comes up is, that my textView is not changing, it always stays the same: 0.

public class MainActivity extends Activity implements OnGestureListener {
    private Paint paint = new Paint();
    public int sco = 0;
    public int colora;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        load();

        TextView score = (TextView) findViewById(R.id.textView1);
        score.setText(String.valueOf(sco));
    }

    private void load() {

        Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bg);
        RelativeLayout ll = (RelativeLayout) findViewById(R.id.rect);
        ll.setBackgroundDrawable(new BitmapDrawable(bg));

        List < Integer > numbers = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW);
        Collections.shuffle(numbers);

        colora = numbers.get(0);
        if (colora == Color.RED) {
            sco++;
        }

        paint.setColor(numbers.get(0));
        canvas.drawRect(20, 15, 11, 3, paint);

        paint.setColor(numbers.get(1));
        canvas.drawRect(19, 15, 15, 3, paint);

        paint.setColor(numbers.get(2));
        canvas.drawRect(5, 5, 15, 35, paint);

        paint.setColor(numbers.get(3));
        canvas.drawRect(5, 15, 26, 1.4, paint);
    }
}

Upvotes: 1

Views: 88

Answers (2)

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

I would move a copy of this:

score.setText(String.valueOf(sco));

into your load method:

    colora = numbers.get(0);
    if (colora == Color.RED) {
        sco++;
        score.setText(String.valueOf(sco));
    }

so everything together would be:

public class MainActivity extends Activity implements OnGestureListener {
    private Paint paint = new Paint();
    public int sco = 0;
    public int colora;
    TextView score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        score = (TextView) findViewById(R.id.textView1);
        score.setText(String.valueOf(sco));

        load();
    }

    private void load() {

        Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bg);
        RelativeLayout ll = (RelativeLayout) findViewById(R.id.rect);
        ll.setBackgroundDrawable(new BitmapDrawable(bg));

        List < Integer > numbers = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW);
        Collections.shuffle(numbers);

        colora = numbers.get(0);
        if (colora == Color.RED) {
            sco++;
            score.setText(String.valueOf(sco));
        }

        paint.setColor(numbers.get(0));
        canvas.drawRect(20, 15, 11, 3, paint);

        paint.setColor(numbers.get(1));
        canvas.drawRect(19, 15, 15, 3, paint);

        paint.setColor(numbers.get(2));
        canvas.drawRect(5, 5, 15, 35, paint);

        paint.setColor(numbers.get(3));
        canvas.drawRect(5, 15, 26, 1.4, paint);
    }
}

Upvotes: 1

Roman Kolomenskii
Roman Kolomenskii

Reputation: 648

Call score.setText(String.valueOf(sco)); every time sco changes. You are only calling it once in onCreate().

Upvotes: 1

Related Questions