简体   繁体   English

尝试在AndroidStudio中绘制矩形矩阵。 没用

[英]Trying to draw a rectangle matrix in AndroidStudio. It doesn't work

I'm programming a mobile game, something like the Arkanoid game. 我正在编写一个手机游戏,类似于Arkanoid游戏。 I've started trying to draw a matrix made by rectangles, so I wrote a "for" which goes over the columns and other "for" within the first one which goes over the rows. 我已经开始尝试绘制一个由矩形组成的矩阵,所以我写了一个“ for”,它遍历各列,而其他“ for”,则遍历各行。 When I execute the App, this just draw an only row. 当我执行该应用程序时,这仅绘制了一行。 I don't know what's wrong. 我不知道怎么了 Help please! 请帮助!

     private void dibujarLadrillos(Canvas canvas, Paint paint)
{
    int width = 0;
    int height = 0;
    Rect[] ladrillos;

    for(int j = 0; j<= 2; j++) {
        ladrillos = new Rect[5];
        for (int i = 0; i <= ladrillos.length - 1; i++) {
            ladrillos[i] = new Rect(width, height, width + getWidth() / 5 - 10, height + getHeight()/10 );
            width += ladrillos[i].width() + 10;
            canvas.drawRect(ladrillos[i], paint);
        }
        height+= ladrillos[0].height() + 10;
    }
}

For each "j" you repeat the "i" loop. 对于每个“ j”,重复“ i”循环。

So all but the last of the new Rect[5] you generate and fill are unreachable for you. 因此,您生成和填充的所有new Rect[5]最后一个new Rect[5] The last one can be accessed via "ladrillos". 可以通过“ ladrillos”访问最后一个。

You need a 2-dimensional array like this: 您需要这样的二维数组:

Rect[][] ladrillos = new Rect[5][5];

The for loop has to be changed accordingly (I'm sure this needs some fine tuning but unfortunately I'm not familiar with Arkanoid ;-) ) for循环必须相应地更改(我确定这需要进行一些微调,但是很遗憾,我对Arkanoid;-并不熟悉))

for(int j = 0; j<= 2; j++) {
    for (int i = 0; i <= ladrillos[j].length - 1; i++) {
        ladrillos[j][i] = ...
        width += ladrillos[j][i].width() + 10;
        canvas.drawRect(ladrillos[j][i], paint);
    }
    height+= ladrillos[j][0].height() + 10;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM