Java me 在j2me迷宫游戏中添加碰撞检测
我正在创建一个迷宫游戏,我正在使用2d阵列绘制迷宫。我画了迷宫,角色在周围移动,但每当角色碰到墙时,我需要添加一些碰撞检测(墙图像由“地图”数组中的数字9表示) 所以基本上我想做的是检测“mQuatsch”是否将移动到值为9的“map”数组的一部分,如果是,就不要移动 有人知道怎么做吗Java me 在j2me迷宫游戏中添加碰撞检测,java-me,collision-detection,midp,lcdui,Java Me,Collision Detection,Midp,Lcdui,我正在创建一个迷宫游戏,我正在使用2d阵列绘制迷宫。我画了迷宫,角色在周围移动,但每当角色碰到墙时,我需要添加一些碰撞检测(墙图像由“地图”数组中的数字9表示) 所以基本上我想做的是检测“mQuatsch”是否将移动到值为9的“map”数组的一部分,如果是,就不要移动 有人知道怎么做吗 import java.io.IOException; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; p
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class QuatschCanvas
extends GameCanvas
implements Runnable {
private boolean mTrucking;
private LayerManager mLayerManager;
private TiledLayer mAtmosphere;
private TiledLayer mBackground;
private int mAnimatedIndex;
private Sprite mQuatsch;
private int mState, mDirection;
private final int DOOR = 1;
private final int WALL = 9;
private final int KEY = 0;
private static final int kStanding = 1;
private static final int kRunning = 2;
private static final int kRunningUp = 3;
private static final int kRunningDown = 4;
private static final int kLeft = 1;
private static final int kRight = 2;
private static final int kUp = 3;
private static final int kDown = 4;
int [][] map = new int [8][7];
private static final int[] kRunningSequence = { 0, 1, 0, 2};
private static final int[] kStandingSequence = { 0 };
private static final int[] kRunningSequenceUp = { 3, 4, 3, 5};
private static final int[] kRunningSequenceDown = { 6, 7, 6, 8};
public QuatschCanvas(String quatschImageName,
String atmosphereImageName, String backgroundImageName)
throws IOException {
super(true);
// Create a LayerManager.
mLayerManager = new LayerManager();
int w = getWidth();
int h = getHeight();
mLayerManager.setViewWindow(96, 0, w, h);
createBackground(backgroundImageName);
createAtmosphere(atmosphereImageName);
createQuatsch(quatschImageName);
}
private void createBackground(String backgroundImageName)
throws IOException {
// Create the tiled layer.
Image backgroundImage = Image.createImage(backgroundImageName);
//first 4 columns
map[0][0] = 9; map[1][0] = 9; map[2][0] = 9; map[3][0] = 9;
map[0][1] = 9; map[1][1] = 4; map[2][1] = 4; map[3][1] = 9;
map[0][2] = 9; map[1][2] = 4; map[2][2] = 4; map[3][2] = 1;
map[0][3] = 9; map[1][3] = 9; map[2][3] = 9; map[3][3] = 9;
map[0][4] = 9; map[1][4] = 4; map[2][4] = 1; map[3][4] = 4;
map[0][5] = 9; map[1][5] = 4; map[2][5] = 9; map[3][5] = 4;
map[0][6] = 9; map[1][6] = 9; map[2][6] = 9; map[3][6] = 9;
//second 4 columns
map[4][0] = 9; map[5][0] = 9; map[6][0] = 9; map[7][0] = 9;
map[4][1] = 4; map[5][1] = 4; map[6][1] = 4; map[7][1] = 9;
map[4][2] = 4; map[5][2] = 4; map[6][2] = 4; map[7][2] = 9;
map[4][3] = 9; map[5][3] = 4; map[6][3] = 9; map[7][3] = 9;
map[4][4] = 4; map[5][4] = 4; map[6][4] = 4; map[7][4] = 9;
map[4][5] = 4; map[5][5] = 4; map[6][5] = 4; map[7][5] = 9;
map[4][6] = 9; map[5][6] = 9; map[6][6] = 9; map[7][6] = 9;
mBackground = new TiledLayer(8, 7, backgroundImage, 96, 96);
mBackground.setPosition(12, 0);
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
int column = i % 8;
int row = j;
mBackground.setCell(column, row, map[i][j]);
}
}
mAnimatedIndex = mBackground.createAnimatedTile(8);
//mBackground.setCell(3, 0, mAnimatedIndex);
//mBackground.setCell(5, 0, mAnimatedIndex);
mLayerManager.append(mBackground);
}
private void createAtmosphere(String atmosphereImageName)
throws IOException {
// Create the atmosphere layer
Image atmosphereImage = Image.createImage(atmosphereImageName);
mAtmosphere = new TiledLayer(8, 1, atmosphereImage,
atmosphereImage.getWidth(), atmosphereImage.getHeight());
mAtmosphere.fillCells(0, 0, 8, 1, 1);
mAtmosphere.setPosition(0,192 );
mLayerManager.insert(mAtmosphere, 0);
}
private void createQuatsch(String quatschImageName)
throws IOException {
// Create the sprite.
Image quatschImage = Image.createImage(quatschImageName);
mQuatsch = new Sprite(quatschImage, 48, 48);
mQuatsch.setPosition(96 + (getWidth() - 48) / 2, 100);
mQuatsch.defineReferencePixel(24, 24);
setDirection(kLeft);
setState(kStanding);
mLayerManager.insert(mQuatsch, 1);
}
public void start() {
mTrucking = true;
Thread t = new Thread(this);
t.start();
}
public void run() {
int w = getWidth();
int h = getHeight();
Graphics g = getGraphics();
int frameCount = 0;
int factor = 2;
int animatedDelta = 0;
while (mTrucking) {
if (isShown()) {
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0) {
setDirection(kLeft);
setState(kRunning);
mBackground.move(3, 0);
mAtmosphere.move(3, 0);
mQuatsch.nextFrame();
}
else if ((keyStates & RIGHT_PRESSED) != 0) {
setDirection(kRight);
setState(kRunning);
mBackground.move(-3, 0);
mAtmosphere.move(-3, 0);
mQuatsch.nextFrame();
}
else if ((keyStates & UP_PRESSED) != 0) {
setDirection(kUp);
setState(kRunningUp);
mBackground.move(0, 3);
mAtmosphere.move(0, 3);
mQuatsch.nextFrame();
}
else if ((keyStates & DOWN_PRESSED) != 0) {
setDirection(kDown);
setState(kRunningDown);
mBackground.move(0, -3);
mAtmosphere.move(0, -3);
mQuatsch.nextFrame();
}
else {
setState(kStanding);
}
frameCount++;
if (frameCount % factor == 0) {
int delta = 1;
if (frameCount / factor < 10) delta = -1;
mAtmosphere.move(delta, 0);
if (frameCount / factor == 20) frameCount = 0;
mBackground.setAnimatedTile(mAnimatedIndex,
8 + animatedDelta++);
if (animatedDelta == 3) animatedDelta = 0;
}
g.setColor(0x5b1793);
g.fillRect(0, 0, w, h);
mLayerManager.paint(g, 0, 0);
flushGraphics();
}
try { Thread.sleep(80); }
catch (InterruptedException ie) {}
}
}
public void stop() {
mTrucking = false;
}
public void setVisible(int layerIndex, boolean show) {
Layer layer = mLayerManager.getLayerAt(layerIndex);
layer.setVisible(show);
}
public boolean isVisible(int layerIndex) {
Layer layer = mLayerManager.getLayerAt(layerIndex);
return layer.isVisible();
}
private void setDirection(int newDirection) {
if (newDirection == mDirection) return;
if (mDirection == kLeft)
mQuatsch.setTransform(Sprite.TRANS_MIRROR);
else if (mDirection == kRight)
mQuatsch.setTransform(Sprite.TRANS_NONE);
mDirection = newDirection;
}
private void setState(int newState) {
if (newState == mState) return;
switch (newState) {
case kStanding:
mQuatsch.setFrameSequence(kStandingSequence);
mQuatsch.setFrame(0);
break;
case kRunning:
mQuatsch.setFrameSequence(kRunningSequence);
break;
case kRunningUp:
mQuatsch.setFrameSequence(kRunningSequenceUp);
break;
case kRunningDown:
mQuatsch.setFrameSequence(kRunningSequenceDown);
break;
default:
break;
}
mState = newState;
}
}
import java.io.IOException;
导入javax.microedition.lcdui.*;
导入javax.microedition.lcdui.game.*;
公共类四元结构
扩展游戏画布
实现可运行{
私家车;
私有层管理器mLayerManager;
私有瓦层mAtmosphere;
私人瓷砖铺层背景;
私人int mAnimatedIndex;
私人斯普雷特·穆瓦茨;
私有int mState,mDirection;
私人最终内部门=1;
私人最终内墙=9;
私有最终整数密钥=0;
专用静态最终int kStanding=1;
私有静态最终整数kRunning=2;
私有静态最终整数kRunningUp=3;
私有静态最终整数kRunningDown=4;
专用静态最终int kLeft=1;
私有静态最终int-kRight=2;
私有静态最终int kUp=3;
私有静态final int kDown=4;
int[]map=新int[8][7];
私有静态final int[]kRunningSequence={0,1,0,2};
私有静态final int[]kStandingSequence={0};
私有静态final int[]kRunningSequenceUp={3,4,3,5};
私有静态final int[]kRunningSequenceDown={6,7,6,8};
公共QuatschCanvas(字符串quatschImageName,
字符串atmosphereImageName、字符串backgroundImageName)
抛出IOException{
超级(真);
//创建图层管理器。
mLayerManager=新层管理器();
int w=getWidth();
inth=getHeight();
mLayerManager.setViewWindow(96,0,w,h);
createBackground(backgroundImageName);
createAtmosphere(atmosphereImageName);
createQuatsch(quatschImageName);
}
私有void createBackground(字符串backgroundImageName)
抛出IOException{
//创建平铺层。
Image backgroundImage=Image.createImage(backgroundImageName);
//前4列
地图[0][0]=9;地图[1][0]=9;地图[2][0]=9;地图[3][0]=9;
地图[0][1]=9;地图[1][1]=4;地图[2][1]=4;地图[3][1]=9;
地图[0][2]=9;地图[1][2]=4;地图[2][2]=4;地图[3][2]=1;
地图[0][3]=9;地图[1][3]=9;地图[2][3]=9;地图[3][3]=9;
地图[0][4]=9;地图[1][4]=4;地图[2][4]=1;地图[3][4]=4;
地图[0][5]=9;地图[1][5]=4;地图[2][5]=9;地图[3][5]=4;
地图[0][6]=9;地图[1][6]=9;地图[2][6]=9;地图[3][6]=9;
//第二个4列
地图[4][0]=9;地图[5][0]=9;地图[6][0]=9;地图[7][0]=9;
地图[4][1]=4;地图[5][1]=4;地图[6][1]=4;地图[7][1]=9;
地图[4][2]=4;地图[5][2]=4;地图[6][2]=4;地图[7][2]=9;
地图[4][3]=9;地图[5][3]=4;地图[6][3]=9;地图[7][3]=9;
地图[4][4]=4;地图[5][4]=4;地图[6][4]=4;地图[7][4]=9;
地图[4][5]=4;地图[5][5]=4;地图[6][5]=4;地图[7][5]=9;
地图[4][6]=9;地图[5][6]=9;地图[6][6]=9;地图[7][6]=9;
mBackground=新的平铺层(8,7,backgroundImage,96,96);
mBackground.设置位置(12,0);
对于(int i=0;i