>this.SelectArea[1] = this.loc[1];//设置光标的竖直位置 //设置光标的宽度 if (this.loc[0] + 1 < Images.WIDTH) { this.SelectArea[2] = this.MyMap.Grid[this.loc[1]][this.loc[0] + 1] != (byte) '1' ? 1 : 2; }else { this.SelectArea[2] = 1; } //设置光标的高度 if (this.loc[1] + 1 < Images.HEIGHT) { this.SelectArea[3] = this.MyMap.Grid[this.loc[1] + 1][this.loc[0]] != (byte) '2' ? 1 : 2; }else { this.SelectArea[3] = 1; } }
private boolean setMoveRange() { //设置要移动到的区域,能够移动返回true,否则返回false for (int i = 0; i < this.SelectArea[2]; i++) { for (int j = 0; j < this.SelectArea[3]; j++) { if (this.loc[1] + j >= Images.HEIGHT || this.loc[0] + i >= Images.WIDTH || (!isInRange(this.loc[0] + i, this.loc[1] + j) && this.MyMap.Grid[this.loc[1] + j][this.loc[0] + i] != Images.BLANK)) { return false; } } } this.MoveArea[0] = this.loc[0]; this.MoveArea[1] = this.loc[1]; this.MoveArea[2] = this.SelectArea[2]; this.MoveArea[3] = this.SelectArea[3]; return true; }
private boolean isInRange(int x, int y) { //判断给定的(x,y)点是否在选定区域之内,x是水平坐标,y是竖直坐标 if (x >= this.SelectArea[0] && x < this.SelectArea[0] + this.SelectArea[2] && y >= this.SelectArea[1] && y < this.SelectArea[1] + this.SelectArea[3]) { return true; }else { return false; } }
private boolean isInRange2(int x, int y) { //判断给定的(x,y)点是否在要移动到的区域之内,x是水平坐标,y是竖直坐标 if (x >= this.MoveArea[0] && x < this.MoveArea[0] + this.MoveArea[2] && y >= this.MoveArea[1] && y < this.MoveArea[1] + this.MoveArea[3]) { return true; }else { return false; } }
protected void keyPressed(int keyCode) { //处理按下键盘的事件,这是Canvas的实例方法 switch (getGameAction(keyCode)) {//将按键的值转化成方向常量 case Canvas.UP://向上 if (!this.selected) {//还没有选定要移动的区域 if (this.loc[1] - 1 >= 0) {//向上还有移动空间 this.loc[1]--;//向上移动一下 setRange();//设置光标移动的区域,该函数能将光标移动到地图主位置 repaint();//重新绘图 } }else {//已经选定了要移动的区域 if (this.loc[1] - 1 >= 0) {//向上还有移动空间 this.loc[1]--;//向上移动一下 if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域 repaint();//重新绘图 }else {//不能移动 this.loc[1]++;//退回来 } } } break; case Canvas.DOWN://向下 if (!this.selected) {//还没有选定要移动的区域 if (this.loc[1] + 1 < Images.HEIGHT) {//向下还有移动空间 if (this.MyMap.Grid[this.loc[1] + 1][this.loc[0]] == Images.DUP){//该图片有两个格高 this.loc[1]++;//向下移动一下 if (this.loc[1] + 1 < Images.HEIGHT) {//向下还有 <A href="file://移">file://移</A>动空间 this.loc[1]++;//向下移动一下 setRange();//设置光标移动的区域, <A href="file://该">file://该</A>函数能将光标移动到地图主位置 repaint();//重新绘图 }else {//向下没有移动空间 this.loc[1]--;//退回来 } }else {//该图片只有一个格高 this.loc[1]++;//向下移动一下 setRange();//设置光标移动的区域, <A href="file://该">file://该</A>函数能将光标移动到地图主位置 repaint();//重新绘图 } }else { } }else {//已经选定了要移动的区域 if (this.loc[1] + 1 < Images.HEIGHT) {//向下还有移动空间 this.loc[1]++;//向下移动一下 if (setMoveRange()) {//能够移动,该函数能够设置要移动到的区域 repaint();//重新绘图 }else {//不能移动 this.loc[1]--;//退回来 } } } break; case Canvas.LEFT://向左 if (!this.selected) {//还没有选定要移动的区域 if (this.loc[0] - 1 >= 0) {//向左还有移动空间 this.lo 此新闻共有8页 上一页 1 2 3 4 5 6 7 8 下一页 |