我有两个2D int数组对象: originalMap和rendedMap,它们包含与拼图游戏地图相对应的数字。 从线程类中,我从类调用Puzzle.java中获取map数组并修改它。
int [][] rendedMap = Puzzle.getRendedMap().getMap();
int [][] originalMap = Puzzle.getMapObj().getMap();
这是我的线程循环:
drawPuzzle(canvas, map );获取画布和数组并绘制地图。
while(running){
if(!holder.getSurface().isValid())
continue;
canvas = holder.lockCanvas();
if(runSolution && movements.size()>0){
executeSolution();
drawPuzzle(canvas, rendedMap);
if(finished){//checks if the execution finished
runSolution=false;
}
if(message.equals("No more actions") ||
message.equals("Out of bounds, try again!")){
System.out.println(message);
drawPuzzle(canvas, originalMap);
}
}else{
drawPuzzle(canvas, originalMap);
}
drawMovements(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
这是我修改数组的方法:
public void executeSolution(){
numOfPlanets = 2;
for(int x=0;x<rendedMap.length-1; x++){
for(int y=0;y<rendedMap[x].length-1; y++){
//Checks where the robot is and saves its initial position
if(rendedMap[x][y]>=7 && rendedMap[x][y]<=9){
initX=x; initY=y; initial=true;
System.out.println("Initial X: "+initX +" Y: "+initY );
direction = rendedMap[x][rendedMap[x].length-1];
System.out.println("Direction: "+direction );
}
}//end inner for
}//end for
if(movements.size()>0){
for(int i=0; i<movements.size();i++){
if(movements.get(i).getColour().equals("none")){
if(movements.get(i).getMotion().equals("straight")){
if(direction==RIGHT){
if(rendedMap[initX][initY+1]==0){
finished=true;
message = "Out of bounds, try again!";
return;
}else{
rendedMap[initX][initY+1]= rendedMap[initX][initY+1]+6;
rendedMap[initX][initY]=rendedMap[initX][initY]-6;
}
}
}//action if
}//color if
}//movements for loop
if(numOfPlanets > 0 ){
finished = true;
message ="No more actions";
}
}//movements if statement
}
我的问题是当我渲染renderMap数组时出于某种原因,原始地图也会发生变化,当我试图回退原始地图时,它不会改变回来。 我甚至打印出来并逐行检查它们是相同的。
任何可能导致此问题的提示? 我花了好几天才弄明白。
答案 0 :(得分:0)
if(direction==RIGHT) {
if(rendedMap[initX][initY+1]==0) {
finished=true;
message = "Out of bounds, try again!";
return;
} else {
rendedMap[initX][initY+1]= rendedMap[initX][initY+1]+6;
rendedMap[initX][initY]=rendedMap[initX][initY]-6;
}
}
检查您的rendedMap
数组是否已初始化,因为这通常会导致上下文错误,尤其是在Android IDE中。
答案 1 :(得分:0)
if(!holder.getSurface().isValid())
continue;
canvas = holder.lockCanvas();
if(runSolution && movements.size()>0){
executeSolution();
drawPuzzle(canvas, rendedMap);
if(finished){//checks if the execution finished
runSolution=false;
}
if(message.equals("No more actions") ||
message.equals("Out of bounds, try again!")){
System.out.println(message);
drawPuzzle(canvas, originalMap);
}
}else{
drawPuzzle(canvas, originalMap);
}
drawMovements(canvas);
回想一下你的drawPuzzle beforte表面视图被锁定,因为其他原因导致它可能无法在该上下文循环中执行,正如我所说的那样,这是一个与我们的组织应用程序共同的问题
希望这有帮助!做hesistate要求更多的帮助
抱歉英语我是印度人