服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

实现游戏开发中的屏幕滚动功能

    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();
    }
}

扫描关注微信公众号