我有一段代码可以达到这种效果:
int pieceX = 0;
int pieceY = 0;
int board[8][47] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
if (pieceX > 0 && pieceY < 46) {
/* If I remove this it doesn't crash */
if (board[pieceX-1][pieceY] == 0 && board[pieceX][pieceY+1] == 0) {
pieceX -= 1;
}
/*-----------------------------------*/
}
据我所知,我正在正确初始化我的数组并且我保持在索引范围内。我不太使用 Processing 或 Arduino,所以我希望它是简单/明显的。
编辑:嗯..我只是用这段代码做了一个简约的测试版本,它不会崩溃。所以,这与本示例中没有的代码有关。该死。将尝试在这些线上归零。(在正确隔离问题代码之前发布此内容很糟糕。)虽然这准确地描述了问题,但它并没有重现它。奇怪的虫子。
编辑2:这不会崩溃:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
}
}
}
这不会崩溃:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
pieceX -= 1;
}
}
这确实崩溃了:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX -= 1;
}
}
}
知道发生了什么吗?ButtonA 永远不会高,所以.. 我正在调整的代码甚至都不重要(一切都验证并上传正常。)
编辑3:这崩溃了:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX -= 1;
}
}
}
这不会:
if (0 == 1) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX -= 1;
}
}
}
这崩溃了:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX = 1;
}
}
}
这不会:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
pieceX = 1;
}
}
这不会:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
}
}
}
编辑,这里是完整的源代码。我只有几个小时才能进入黑白博士马里奥克隆。我从来没有用这种语言写过,所以..可能有点草率。更多的是处理/视频游戏硬件/arduino中的随机学习实验。