问题描述
我以画线迷宫使用一对布尔数组的(水平和垂直)目前正在创建一个迷宫。
I am currently creating a maze using a pair of boolean array (horizontal and vertical) in order to draw lines for the maze.
迷宫每天只有5显示从一次阵列的bool。然后,我有谁是始终围绕一个用户,并为他的动作穿过迷宫下一组的bool的绘制。这是工作,因为它应该。
The maze only every displays 5 bools from the array at one time. Then, I have an user who is always centered and as he moves through the maze the next set of bools are drawn. This is working as it should.
这是我遇到的问题是:当用户移动到迷宫的某一部分的for循环绘制线条变得比布尔数组较高,因此崩溃的应用程序。请在下面找到一些code片段。
The issue that I am having is: when the user moves to a certain part of the maze the for loop drawing the lines becomes higher than the bool array and therefore crashes the app. Please find below some code snippets.
本的onDraw:
protected void onDraw(Canvas canvas) {
canvas.drawRect(0, 0, width, height, background);
int currentX = maze.getCurrentX(),currentY = maze.getCurrentY();
int drawSizeX = 6 + currentX;
int drawSizeY = 6 + currentY;
currentX = currentX - 2;
currentY = currentY - 2;
for(int i = 0; i < drawSizeX - 1; i++) {
for(int j = 0; j < drawSizeY - 1; j++) {
float x = j * totalCellWidth;
float y = i * totalCellHeight;
if(vLines[i + currentY][j + currentX]) {
canvas.drawLine(x + cellWidth, //start X
y, //start Y
x + cellWidth, //stop X
y + cellHeight, //stop Y
line);
}
if(hLines[i + currentY][j + currentX]) {
canvas.drawLine(x, //startX
y + cellHeight, //startY
x + cellWidth, //stopX
y + cellHeight, //stopY
line);
}
}
//draw the user ball
canvas.drawCircle((2 * totalCellWidth)+(cellWidth/2), //x of center
(2 * totalCellHeight)+(cellWidth/2), //y of center
(cellWidth*0.45f), //radius
ball);
}
编辑1 - 举 -
EDIT 1 - The Move -
public boolean move(int direction) {
boolean moved = false;
if(direction == UP) {
if(currentY != 0 && !horizontalLines[currentY-1][currentX]) {
currentY--;
moved = true;
}
}
if(direction == DOWN) {
if(currentY != sizeY-1 && !horizontalLines[currentY][currentX]) {
currentY++;
moved = true;
}
}
if(direction == RIGHT) {
if(currentX != sizeX-1 && !verticalLines[currentY][currentX]) {
currentX++;
moved = true;
}
}
if(direction == LEFT) {
if(currentX != 0 && !verticalLines[currentY][currentX-1]) {
currentX--;
moved = true;
}
}
if(moved) {
if(currentX == finalX && currentY == finalY) {
gameComplete = true;
}
}
return moved;
}
如果有其他任何我需要澄清,请让我知道。
If there is anything else that I need to clarify please let me know.
先谢谢了。
推荐答案
drawSizeX / Y阵列上索引时currentX / Y足够高(长-6)
drawSizeX/Y indexes over the array when currentX/Y is high enough (length-6)
所以限制值Math.min(电流+ 6,array.length)
So limit the values to Math.min(current + 6, array.length)
这篇关于帆布的OnDraw方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!