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.

4

1 回答 1

0

看起来我没有在每次 onDraw 调用时将 paintY 重置为 0,并且它正在正确绘制,但在屏幕下方。就像我说的那样,可能而且曾经是我的愚蠢:)。

于 2018-02-13T14:57:36.277 回答