我正在创建一个迷宫游戏,我正在使用二维数组来绘制迷宫。我绘制了迷宫并且角色在这个地方移动但我需要在角色撞到墙壁时添加一些碰撞检测(墙面图像由“地图”数组中的数字9表示)。
基本上我想做的是检测“mQuatsch”是否会进入“map”数组的一部分,值为9,如果是,请不要移动
有谁知道怎么做?
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;
}
}
答案 0 :(得分:1)
看看这个: http://j2medevcorner.wordpress.com/2007/03/29/collision-detection-in-j2me/
(我认为我们不需要所有代码来回答这个问题)。