igorcar1994
igorcar1994

Reputation: 67

Q:Android - How to increase score every time I click on screen

For some reason, whenever I increase "score" in 1st class(in OnTouch method) it won't update in my 2nd class when I call drawText method.The score, however, increases when I touch the screen, but calling zc.getScore() method in 2nd class stil shows 100.

ZmijicaSV zmija;
private int score=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    zmija=new ZmijicaSV(this);
    zmija.setOnTouchListener(this);
    setContentView(zmija);

}

@Override
protected void onPause() {
    super.onPause();
    zmija.pause();
}

@Override
protected void onResume() {
    super.onResume();
    zmija.resume();
}


@Override
public boolean onTouch(View v, MotionEvent event) {

    boolean up=zmija.sp.isUp();
    boolean right=zmija.sp.isRight();
    boolean down=zmija.sp.isDown();
    boolean left=zmija.sp.isLeft();


    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            if(up) {
                zmija.sp.setUp(false);
                zmija.sp.setRight(true);
                update();
                break;
            }

            if(right) {
                zmija.sp.setRight(false);
                zmija.sp.setDown(true);
                update();
                break;
            }

            if(down) {
                zmija.sp.setDown(false);
                zmija.sp.setLeft(true);
                update();
                break;
            }
            if(left) {
                zmija.sp.setLeft(false);
                zmija.sp.setUp(true);
                update();
            break;
        }


    }
        return true;
    }

public void update()
{
    score++;
}
public int getScore()
{
    return score;
}

.

Thread t=null;
Pravougaonik pr;
SurfaceHolder holder;
boolean isRunning=false;
SnakeParts sp;
ZmijicaCrtanje zc;
public ZmijicaSV(Context context) {
    super(context);
    holder=getHolder();
    pr=new Pravougaonik(context);
    sp=new SnakeParts();
    zc=new ZmijicaCrtanje();
}

public void run()
{
    Paint p=new Paint();
    p.setColor(Color.BLUE);
    p.setStyle(Paint.Style.FILL);
     while(isRunning)
     {

         if(!holder.getSurface().isValid()) {
             continue;
         }
         Canvas c=holder.lockCanvas();
         c.drawColor(Color.BLACK);
         sp.Draw(c);
         pr.onDraw(c);
         sp.update();
         drawText(c);
         holder.unlockCanvasAndPost(c);

         try {

            t.sleep(50);

         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
}

public void resume()
{
    isRunning=true;
    t=new Thread(this);
    t.start();
}

public void pause()
{
    isRunning=false;
    while(true)
    {
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        break;
    }
    t=null;

}

public void drawText(Canvas canvas)
{
    Paint paint=new Paint();
    paint.setColor(Color.YELLOW);
    paint.setTextSize(200);
    canvas.drawText("Score:"+zc.getScore(),100,getHeight()-200,paint);

}

Upvotes: 1

Views: 119

Answers (1)

Jochem Kleine
Jochem Kleine

Reputation: 186

I can't know for sure, because you haven't included the name of the first class, but im quite sure the problem is that you are creating a new instance of the first class (probabaly ZmijicaCrtanje()) in your second class. This instance of ZmijicaCrtanje will be unaltered by the clicks registered in the first instance of ZmijicaCrtanje.

Two possible ways to fix this error are:

  1. Make score a static variable.
  2. Pass an instance of ZmijicaCrtanje to the constructor of the second class, ZmijicaSV, and set it equal to the global variable zc.

Good luck!

Upvotes: 1

Related Questions