1 private void swipeUp(){ //向上滑动函数 3 for (int x = 0; x < 4; x++) { //行从上到下 4 for (int y = 0; y < 4; y++) { //列从左到右 5 for (int y1 = y+1; y1 < 4; y1++) { //当前方块的下一个向底遍历 6 if (Matrix[x][y1].getNum()>0) { //如果遍历过程中有非空方块(即有数字),则执行操作 7 if (Matrix[x][y].getNum()<=0) { //如果当前方块是空值,则将遍历到的非空方块移动到当前方块 9 int yoy =y1 - y; //移动的步距值 10 ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0, 11 Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,-yoy); //-yoy为负数,即相对于0,在y方向产生向上移动yoy步距的动画 12 ta.setDuration(100); //动画持续时间 13 Matrix[x][y1].startAnimation(ta); //演示动画 15 Matrix[x][y].setNum(Matrix[x][y1].getNum()); //设置当前方块值,移动成功 16 Matrix[x][y1].setNum(0); //将遍历到的方块值清零,即不显示 17 y--; //break后重新从当前方块的下一个重新开始遍历,等待一次寻找到相同值且合并的机会
break; //break后重新从当前方块的下一个重新开始遍历,等待一次寻找到相同值且合并的机会 20 }else if (Matrix[x][y].equals(Matrix[x][y1])) { //如果当前方块的值与遍历到的方块值相等 22 int yoy =y1 - y; //移动的步距值 23 ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0, 24 Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,-yoy); //-yoy为负数,即相对于0,在y方向产生向上移动yoy步距的动画 25 ta.setDuration(100); //动画持续时间 26 Matrix[x][y1].startAnimation(ta); //演示动画 28 Matrix[x][y].setNum(Matrix[x][y1].getNum()*2); //设置当前方块值为两倍,即两方块相加 29 Matrix[x][y1].setNum(0); //将遍历到的方块清零,即不显示
break; //完成相加后,从下一个方块开始完成新一轮操作 33 }else { 34 break; //如果当前方块的值与遍历到的方块值不相等,则从下一个方块开始完成新一轮操作 35 } 36 } 37 } 38 } 39 } 41 } 42 43 private void swipeDown(){ //向下滑动函数 45 for (int x = 0; x < 4; x++) { 46 for (int y = 3; y >= 0; y--) { 47 for (int y1 = y-1; y1 >= 0; y1--) { 48 if (Matrix[x][y1].getNum()>0) { 49 if (Matrix[x][y].getNum()<=0) { 51 int yoy =y1 - y; 52 ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0, 53 Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,-yoy); 54 ta.setDuration(100); 55 Matrix[x][y1].startAnimation(ta); 57 Matrix[x][y].setNum(Matrix[x][y1].getNum()); 58 Matrix[x][y1].setNum(0); 59 y++;
break; 62 }else if (Matrix[x][y].equals(Matrix[x][y1])) { 64 int yoy =y1 - y; 65 ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0, 66 Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,-yoy); 67 ta.setDuration(100); 68 Matrix[x][y1].startAnimation(ta); 70 Matrix[x][y].setNum(Matrix[x][y1].getNum()*2); 71 Matrix[x][y1].setNum(0);
break; 75 }else { 76 break; 77 } 78 } 79 } 80 } 81 } 83 } 84 85 private void swipeLeft(){ //向左滑动函数 87 for (int y = 0; y < 4; y++) { 88 for (int x = 0; x < 4; x++) { 89 for (int x1 = x+1; x1 < 4; x1++) { 90 if (Matrix[x1][y].getNum()>0) { 91 if (Matrix[x][y].getNum()<=0) { 93 int xox =x1 - x; 94 ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,-xox, 95 Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0); 96 ta.setDuration(100); 97 Matrix[x1][y].startAnimation(ta); 99 Matrix[x][y].setNum(Matrix[x1][y].getNum()); 100 Matrix[x1][y].setNum(0); 101 x--;
break; 104 }else if (Matrix[x][y].equals(Matrix[x1][y])) { 106 int xox =x1 - x; 107 ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,-xox, 108 Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0); 109 ta.setDuration(100); 110 Matrix[x1][y].startAnimation(ta); 112 Matrix[x][y].setNum(Matrix[x1][y].getNum()*2); 113 Matrix[x1][y].setNum(0);
break; 117 }else { 118 break; 119 } 120 } 121 } 122 } 123 } 125 } 126 127 private void swipeRight(){ //向右滑动函数 129 for (int y = 0; y < 4; y++) { 130 for (int x = 3; x >= 0; x--) { 131 for (int x1 = x-1; x1 >= 0; x1--) { 132 if (Matrix[x1][y].getNum()>0) { 133 if (Matrix[x][y].getNum()<=0) { 135 int xox =x1 - x; 136 ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,-xox, 137 Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0); 138 ta.setDuration(100); 139 Matrix[x1][y].startAnimation(ta); 141 Matrix[x][y].setNum(Matrix[x1][y].getNum()); 142 Matrix[x1][y].setNum(0); 143 x++;
break; 146 }else if (Matrix[x][y].equals(Matrix[x1][y])) { 148 int xox =x1 - x; 149 ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,-xox, 150 Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0); 151 ta.setDuration(100); 152 Matrix[x1][y].startAnimation(ta); 154 Matrix[x][y].setNum(Matrix[x1][y].getNum()*2); 155 Matrix[x1][y].setNum(0);
break; 159 }else { 160 break; 161 } 162 } 163 } 164 } 165 } 167 }