J2ME游戏开发中使用层的概念中介绍了如何在游戏开发中使用层,其中提到LayerManager的一个概念,View Window,本文将借助这个概念实现屏幕滚动的功能。

    屏幕的移动效果一般我们是通过改变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

    // Sprites to be used
    Image backgroundImage;
    private Sprite backgroundSprite;

    // Layer Manager
    private LayerManager layerManager;

    // Constructor and initialization
    public ExampleGameCanvas() throws Exception
    {
        super(true);
        width = getWidth();
        height = getHeight();

        scnX = 55;
        scnY = 20;
        delay = 20;

        // Load Images to Sprites
        backgroundImage = Image.createImage("/background.png");
        backgroundSprite = new Sprite(backgroundImage);

        layerManager = new LayerManager();
        layerManager.append(backgroundSprite);

    }

    // Automatically start thread for game loop
    public void start()
    {
        isPlay = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void stop()
    {
        isPlay = false;
    }

    // Main Game Loop
    public void run()
    {
        Graphics g = getGraphics();
        while (isPlay == true)
        {

            input();
            drawScreen(g);
            try
            {
                Thread.sleep(delay);
            } catch (InterruptedException ie)
            {
            }
        }
    }

    // Method to Handle User Inputs
    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.midlet.*;
import javax.microedition.lcdui.*;

public class SimpleScrollingLayerManger extends MIDlet
{
    private Display display;

    public void startApp()
    {
        try
        {
            display = Display.getDisplay(this);
            ExampleGameCanvas gameCanvas = new ExampleGameCanvas();
            gameCanvas.start();
            display.setCurrent(gameCanvas);
        } catch (Exception ex)
        {
            System.out.println(ex);
        }
    }

    public Display getDisplay()
    {
        return display;
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
        exit();
    }

    public void exit()
    {
        System.gc();
        destroyApp(false);
        notifyDestroyed();
    }
}