当前位置:编程学习 > JAVA >>

安卓游戏“返回键”求大神指导,急啊!

本人在学安卓,自己在书上复制了一个游戏的代码,发现很不完善,就打算完善这个游戏,现在想为这个游戏设置多一个返回主菜单的按钮。
这个游戏原本是没有返回这个功能的,也就是说,开始游戏后,如果想返回游戏主菜单(即,刚进游戏的菜单栏界面),就要回到安卓的主窗口,再进去,重新进入游戏才可以。
以下是我修改后的代码。
package com.peiandsky;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.View;

public class GamePainter {

public short[][] currentGameData = null;
public int currentDirection = 0;
public boolean moveFlag = false;

public int[] stPos = new int[2];
public int[] backupPos = new int[2];

public void init(int level) {
// 设置游戏地图数据
this.setCurretnGameData(level);
// 寻找玩家方块的位置
for (int i = 0; i < currentGameData.length; i++) {
for (int j = 0; j < currentGameData[i].length; j++) {
if (currentGameData[i][j] == 3) {
stPos[0] = j;
stPos[1] = i;
currentGameData[i][j] = 0;
break;
}
}
}
// 备份玩家方块
backupPos[0] = stPos[0];
backupPos[1] = stPos[1];
// 方向设置为0
currentDirection = 0;
// 移动状态设置为不移动
moveFlag = false;
}

public void over() {
// 设置移动标志为不移动
moveFlag = false;
System.out.println("over!!!");
// 初始化游戏数据
init(GameData.level);
}

public void stop() {
moveFlag = false;
}

public void pass() {
moveFlag = false;
GameData.level++;
// TODO 判断是否超过最大关数
init(GameData.level);
System.out.println("pass!!!");
}

private void setCurretnGameData(int level) {
// 获取游戏数据
short[][] mapData = GameData.mapData[level];
int row = mapData.length;
int col = mapData[0].length;
// 将游戏数据拷贝过来
currentGameData = new short[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
currentGameData[i][j] = mapData[i][j];
}
}

}

public void setMoveDirection(int dir) {
if (currentDirection != 0 && currentDirection != dir) {
currentGameData[stPos[1]][stPos[0]] = 18;
}
currentDirection = dir;
moveFlag = true;
}

// 判断是不是障碍物的方法
public boolean isBlock(int col, int row) {
int b = currentGameData[row][col];
switch (b) {
case 4:
currentGameData[row][col] = 0;
case 1:
case 5:
case 7:
case 10:
case 12:
case 17:
return true;

}
return false;
}

public void checkChangDirBlock(int col, int row) {
// 获取要该表方向的方块的索引
int b = currentGameData[row][col];
// 根据不同的索引反射不同的方向
switch (b) {
case 13:
if (currentDirection == GameData.RIGHT) {
currentDirection = GameData.UP;
}
if (currentDirection == GameData.DOWN) {
currentDirection = GameData.LEFT;
}
break;
case 14:
if (currentDirection == GameData.LEFT) {
currentDirection = GameData.UP;
}
if (currentDirection == GameData.DOWN) {
currentDirection = GameData.RIGHT;
}
break;
case 15:
if (currentDirection == GameData.RIGHT) {
currentDirection = GameData.DOWN;
}
if (currentDirection == GameData.UP) {
currentDirection = GameData.LEFT;
}
break;
case 16:
if (currentDirection == GameData.LEFT) {
currentDirection = GameData.DOWN;
}
if (currentDirection == GameData.UP) {
currentDirection = GameData.RIGHT;
}
break;
}

}

public void gameLogic() {
// 判断玩家是否在移动状态
if (!moveFlag) {
return;
}
// 根据给定的方向移动玩家的位置,并相应的给路径赋值
switch (currentDirection) {
case GameData.UP:
if (currentGameData[stPos[1]][stPos[0]] == 0
|| currentGameData[stPos[1]][stPos[0]] == 3)
currentGameData[stPos[1]][stPos[0]] = 19;
stPos[1]--;
if (stPos[1] < 0) {
// 玩家出界,重新初始本关游戏
over();
}
break;
case GameData.DOWN:
if (currentGameData[stPos[1]][stPos[0]] == 0
|| currentGameData[stPos[1]][stPos[0]] == 3)
currentGameData[stPos[1]][stPos[0]] = 19;
stPos[1]++;
if (stPos[1] >= GameData.maxRow) {
// 玩家出界,重新初始本关游戏
over();
}
break;
case GameData.LEFT:
if (currentGameData[stPos[1]][stPos[0]] == 0
|| currentGameData[stPos[1]][stPos[0]] == 3)
currentGameData[stPos[1]][stPos[0]] = 20;
stPos[0]--;
if (stPos[0] < 0) {
// 玩家出界,重新初始本关游戏
over();
}
break;
case GameData.RIGHT:
if (currentGameData[stPos[1]][stPos[0]] == 0
|| currentGameData[stPos[1]][stPos[0]] == 3)
currentGameData[stPos[1]][stPos[0]] = 20;
stPos[0]++;
if (stPos[0] >= GameData.maxCol) {
// 玩家出界,重新初始本关游戏
over();
}
break;
}
// 前方为空白,绘制玩家
if (currentGameData[stPos[1]][stPos[0]] == 0) {
currentGameData[stPos[1]][stPos[0]] = 3;
}
// 前方为方向方块,根据方向方块的数据切换方向
checkChangDirBlock(stPos[0], stPos[1]);
// 前方为阻挡块
if (isBlock(stPos[0], stPos[1])) {
stop();
switch (currentDirection) {
case GameData.UP:
currentGameData[stPos[1] += 1][stPos[0]] = 3;
break;
case GameData.DOWN:
currentGameData[stPos[1] -= 1][stPos[0]] = 3;
break;
case GameData.LEFT:
currentGameData[stPos[1]][stPos[0] += 1] = 3;
break;
case GameData.RIGHT:
currentGameData[stPos[1]][stPos[0] -= 1] = 3;
break;
}
}
// 前方为收缩盒子
if (currentGameData[stPos[1]][stPos[0]] == 11) {
// 盒子收缩
currentGameData[stPos[1]][stPos[0] - 1] = 0;
currentGameData[stPos[1]][stPos[0] + 1] = 0;
currentGameData[stPos[1]][stPos[0]] = 17;
}
// 前方为传送

// 前方为终点
if (currentGameData[stPos[1]][stPos[0]] == 2) {
System.out.println("通过本关!");
// 进入下一关,初始化下一关数据
pass();

}
}

public void paintGameData(Canvas canvas, Bitmap bitmap) {
short[][] mapdata = currentGameData;
Rect src = new Rect();
Rect dst = new Rect();
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.STROKE);
for (int i = 0; i < mapdata.length; i++) {
for (int j = 0; j < mapdata[i].length; j++) {
int index = mapdata[i][j] - 1;
src.set(index * 22, 0, index * 22 + 22, 22);
dst.set(6 + j * 22, 9 + i * 22, 28 + j * 22, 31 + i * 22);
// 绘制白色方格
canvas.drawRect(dst, paint);
if (index < 0) {
continue;
}
// 绘制障碍物
canvas.drawBitmap(bitmap, src, dst, null);
}
}
// 绘制玩家方块
paintSquare(canvas, bitmap);

}

private void paintSquare(Canvas canvas, Bitmap bitmap) {
Rect src = new Rect();
Rect dst = new Rect();
src.set(2 * 22, 0, 2 * 22 + 22, 22);
// 通过stPos玩家坐标来绘制玩家方块
dst.set(6 + stPos[0] * 22, 9 + stPos[1] * 22, 28 + stPos[0] * 22,
31 + stPos[1] * 22);
canvas.drawBitmap(bitmap, src, dst, null);
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
canvas.drawText("level:" + GameData.level, 30, 50, paint);
canvas.drawText("菜单" , 30, 380, paint); }

public boolean onTouchEvent(MotionEvent event){
int x =(int)event.getX();
int y =(int)event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
if(x>10&&x<50&&y>360&&y<400){
SquareActivity.instance.handler
.sendEmptyMessage(SquareActivity.MENU);
}

}
return true;

}

}
红色的,就是我自己加上去的代码,我是模仿着前面的主菜单栏的代码来写的。但是,这个按钮无法执行。设置断点的话,我不会弄,那个真心不懂怎么设置。 --------------------编程问答-------------------- 看你代码没有这个按钮调用onTouchEvent的方法啊。
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,