屏幕的移動效果一般我們是通過改變View Window的的位置來實現的,比如你想屏幕向右移動,那么你要調整View Window的x坐標增加相應的數值,假如想屏幕向左移動,那么調整View Window的x坐標減少相應的數值。上下移動原理一樣。我們在得到用戶的輸入后就可以對View Window的位置進行調整然后重新繪制屏幕。
PRivate void input()
{
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0)
{
if (scnX - 1 > 0)
scnX--;
}
if ((keyStates & RIGHT_PRESSED) != 0)
{
if (scnX + 1 + 140 < backgroundImage.getWidth())
scnX++;
}
}
// Method to Display Graphics
private void drawScreen(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
// display all layers
layerManager.setViewWindow(scnX, scnY, 140, 140);
layerManager.paint(g, 20, 20);
flushGraphics();
}
我們只使用一個背景圖片如下:
由于程序比較簡單,這里直接給出源代碼不做過多的解釋。
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class ExampleGameCanvas extends GameCanvas implements Runnable
{
private boolean isPlay; // Game Loop runs when isPlay is true
private long delay; // To give thread consistency
private int width; // To hold screen width
private int height; // To hold screen height
private int scnX, scnY; // To hold screen starting viewpoint
新聞熱點
疑難解答