0

i'm struggling with this for a while, not sure if because almost 2 years of absence in Android DEV or my stupidity, I've tried everything and just cannot redraw my screen even if invalidate() is happening. Here's some code:

GameActivity.java

onCreate

...

final CanvasActivity mCanvasActivity = new CanvasActivity(this);
setContentView(mCanvasActivity);

mCanvasActivity.setOnTouchListener(new OnSwipeTouchListener(this) {
        @Override
        public void onSwipeTop() {

            tilesArray[playerPositionY][playerPositionX] = 0;
            playerPositionY--;
            tilesArray[playerPositionY][playerPositionX] = 2;

            mCanvasActivity.invalidate();

        }

CanvasActivity.java

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    movePlayer(canvas);
    Log.e("player", "x " + playerPositionX + " y " + playerPositionY);
}

movePlayer

if (currentBlock == 0) {
                canvas.drawRect(posLeft, posTop, posRight, posBottom, paintWall);
            } else if (currentBlock == 1) {
                canvas.drawRect(posLeft, posTop, posRight, posBottom, paintLabirynth);
            } else if (currentBlock == 3) {
                canvas.drawRect(posLeft, posTop, posRight, posBottom, paintExit);
            } else {
                canvas.drawRect(posLeft, posTop, posRight, posBottom, paintCharacter);
            }

So basically, when We swipe to the top, position is being changed by movePlayer (decreasing y in 2d array). Then, every rectangle on screen is being redrawn (whole screen has only rectangles which are drawn with different colors according to array line by line, doesn't matter I suppose). My variables are changing properly, so invalidate() is firing onDraw(), however there's no change on the screen. Any help much appreciated.

cyrial
  • 163
  • 1
  • 8
  • I suppose CanvasActivity extends View, right? – colens Feb 12 '18 at 21:16
  • Correct. public class CanvasActivity extends View – cyrial Feb 12 '18 at 21:45
  • Well, I don't see anything wrong with onDraw(). When you call invalidate(), onDraw() gets fired, and consequently it calls movePlayer(). It should work if the other math is correct (the way you get posLeft, posTop, etc). Sorry, can't be much of a help. – colens Feb 12 '18 at 22:16
  • That's the thing I don' see any error either. Math is fine, changes are visible in log. I've managed to redraw screen only by doing recreate() keeping position values, but that doesn' make sense to do it such way. – cyrial Feb 13 '18 at 06:17

1 Answers1

0

Looks like I didn't reset paintY to 0 every onDraw call and it was painting properly, but below the screen. Like I said, could be, and was, my stupidity :).

cyrial
  • 163
  • 1
  • 8