2012-02-07 61 views
1

我正在創建一個迷宮遊戲,並使用2d數組繪製迷宮。我繪製了迷宮,角色在這個地方移動,但是當角色碰到牆壁時,我需要添加一些碰撞檢測(牆圖像由「地圖」數組中的數字9表示)。將衝突檢測添加到j2me迷宮遊戲

所以基本上我想要做的是檢測是否「mQuatsch」將進入與值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; 
} 
} 

回答

1

在此請看: http://j2medevcorner.wordpress.com/2007/03/29/collision-detection-in-j2me/

(我不認爲所有的代碼是需要我們來回答這個問題)。

+0

我明白碰撞檢測的概念,它是我遇到的編碼問題,我不知道我應該使用哪個mQuatsch.collidiesWith()方法,以及如何檢查玩家是否要進入零件值爲9的地圖 – 2012-02-07 15:48:02

+0

我認爲你應該使用一個採用'TiledLayer' http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/game/TiledLayer的地圖。 html – vulkanino 2012-02-07 15:53:58

+0

,但是您是否知道如何實現它,以便在您進入具有特定值的單元格時檢測到衝突,如「if(mQuatsch.collidesWith(mBackground.getCell(4,7),true)」 – 2012-02-07 16:02:36