我需要的&QUOT code进行解释;在开始的Andr​​oid游戏" [英] I need a explanation for code of "beginning android games"

查看:118
本文介绍了我需要的&QUOT code进行解释;在开始的Andr​​oid游戏"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的书开始的Andr​​oid游戏,因为过去几天。但我坚持要了解code。

您可以检查或从这里下载code:
HTTP://$c$c.google.com/p/beginnginandroidgames2/downloads /列表

我的意思是该项目是 CH06-MR-妈妈。该活动被称为 MrNomGame

 公共抽象类AndroidGame扩展活动实现了游戏{    AndroidFastRenderView的RenderView;
    图形显卡;
    Audio音频;
    输入输入;
    FileIO专注FILEIO;
    屏幕屏幕;
    激活锁定激活锁定;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);        布尔isLandscape = getResources()getConfiguration()==方向Configuration.ORIENTATION_LANDSCAPE。;
        INT frameBufferWidth = isLandscape? 480:320;
        INT frameBufferHeight = isLandscape? 320:480;
        位图的帧缓冲= Bitmap.createBitmap(frameBufferWidth,
                frameBufferHeight,Config.RGB_565);        漂浮的scaleX =(浮点)frameBufferWidth
                。/ getWindowManager()getDefaultDisplay()的getWidth()。
        漂浮的scaleY =(浮点)frameBufferHeight
                。/ getWindowManager()getDefaultDisplay()的getHeight()。        的RenderView =新AndroidFastRenderView(这一点,FRAMEBUFFER);
        显卡=新AndroidGraphics(getAssets(),帧缓冲);
        FILEIO =新AndroidFileIO(本);
        音频=新AndroidAudio(本);
        输入=新AndroidInput(这一点,的RenderView,将scaleX,的scaleY);
        屏幕= getStartScreen();
        的setContentView(的RenderView);        电源管理电源管理=(电源管理)getSystemService(Context.POWER_SERVICE);
        激活锁定= powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCKGLGame);
    }    @覆盖
    公共无效onResume(){
        super.onResume();
        wakeLock.acquire();
        screen.resume();
        renderView.resume();
    }    @覆盖
    公共无效的onPause(){
        super.onPause();
        wakeLock.release();
        renderView.pause();
        screen.pause();        如果(isFinishing())
            screen.dispose();
    }    公输getInput(){
        返回输入;
    }    公共FileIO专注getFileIO(){
        返回FILEIO;
    }    公共图形的getGraphics(){
        返回显卡;
    }    公共音频getAudio(){
        音频恢复;
    }    公共无效setScreen(屏幕画面){
        如果(大屏幕== NULL)
            抛出新抛出:IllegalArgumentException(屏幕不能为空);        this.screen.pause();
        this.screen.dispose();
        screen.resume();
        screen.update(0);
        this.screen =屏幕;
    }    公共屏幕getCurrentScreen(){
        返回屏;
    }
}

类实现接口游戏

 包com.badlogic.androidgames.framework;公共接口游戏{
    公输getInput();
    公共FileIO专注getFileIO();
    公共图形的getGraphics();
    公共音频getAudio();
    公共无效setScreen(屏幕画面);
    公共屏幕getCurrentScreen();
    公共屏幕getStartScreen();
}

我的第一个问题是:我错过了接口游戏 getStartScreen()的实施code>。通常我要实现一个接口的所有方法。

不管怎样,现在的的onCreate 正在运行。在这一行:

 屏幕= getStartScreen();

程序返回到原点类 MrNomGame ,其中方法 getStartScreen()给出的对象 LoadingScreen 来变量屏幕。类 LoadingScreen

 公共类LoadingScreen扩展屏幕{    公共LoadingScreen(Game游戏){
        超(游戏);
    }    公共无效更新(浮动deltaTime){
        图形G = game.getGraphics();
        Assets.background = g.newPixmap(background.png,PixmapFormat.RGB565);
        Assets.logo = g.newPixmap(为Logo.png,PixmapFormat.ARGB4444);
        Assets.mainMenu = g.newPixmap(mainmenu.png,PixmapFormat.ARGB4444);
        Assets.buttons = g.newPixmap(buttons.png,PixmapFormat.ARGB4444);
        Assets.help1 = g.newPixmap(help1.png,PixmapFormat.ARGB4444);
        Assets.help2 = g.newPixmap(help2.png,PixmapFormat.ARGB4444);
        Assets.help3 = g.newPixmap(help3.png,PixmapFormat.ARGB4444);
        Assets.numbers = g.newPixmap(numbers.png,PixmapFormat.ARGB4444);
        Assets.ready = g.newPixmap(ready.png,PixmapFormat.ARGB4444);
        Assets.pause = g.newPixmap(pausemenu.png,PixmapFormat.ARGB4444);
        Assets.gameOver = g.newPixmap(gameover.png,PixmapFormat.ARGB4444);
        Assets.headUp = g.newPixmap(headup.png,PixmapFormat.ARGB4444);
        Assets.headLeft = g.newPixmap(headleft.png,PixmapFormat.ARGB4444);
        Assets.headDown = g.newPixmap(headdown.png,PixmapFormat.ARGB4444);
        Assets.headRight = g.newPixmap(headright.png,PixmapFormat.ARGB4444);
        Assets.tail = g.newPixmap(tail.png,PixmapFormat.ARGB4444);
        Assets.stain1 = g.newPixmap(stain1.png,PixmapFormat.ARGB4444);
        Assets.stain2 = g.newPixmap(stain2.png,PixmapFormat.ARGB4444);
        Assets.stain3 = g.newPixmap(stain3.png,PixmapFormat.ARGB4444);
        Assets.click = game.getAudio()newSound(click.ogg);
        Assets.eat = game.getAudio()newSound(eat.ogg);
        Assets.bitten = game.getAudio()newSound(bitten.ogg);
        Settings.load(game.getFileIO());
        game.setScreen(新MainMenuScreen(游戏));
    }    公共无效present(浮动deltaTime){    }    公共无效暂停(){    }    公共无效简历(){    }    公共无效的Dispose(){    }
}

屏幕

 公共抽象类屏幕{
    保护最后一场比赛的游戏;    公共屏幕(Game游戏){
        this.game =游戏;
    }    公共抽象无效更新(浮动deltaTime);
    公共抽象无效present(浮动deltaTime);
    公共抽象无效暂停();
    公共抽象无效简历();
    公共抽象无效的Dispose();
}

在一定时间后,的onCreate()方法完成。但在调试模式我的手机的屏幕已经是又黑又暗。这似乎是该方案是在一个循环。我可以经常因为我想preSS F6。但是,当我恢复主菜单会显示。如果您在code看看你会看到主菜单将在更新调用()的方法 loadingScreen

我的问题是:的onCreate()方法之后,会发生什么结束,程序是如何进入更新() loadingScreen <?/ p方法>

我知道这是一个很大code的。但是,这将是非常有用的,我明白了。


解决方案

有关你的第一个问题: AndroidGame 摘要的类。这意味着它没有实现的所有方法游戏,只要延长类之一 AndroidGame 做这个。正如你可以从code看到,这是在做 MrNomGame

 公共类MrNomGame扩展AndroidGame {
    公共屏幕getStartScreen(){
        返回新LoadingScreen(本);
    }
}

现在你的大问题:

的onCreate ,创建一个渲染器:

 的RenderView =新AndroidFastRenderView(这一点,FRAMEBUFFER);

如果你看一下这个类,你会看到 onResume 创建运行下列code一个新的线程:

 公共无效的run(){
    矩形dstRect =新的矩形();
    长STARTTIME = System.nanoTime();
    而(运行){
        如果(!holder.getSurface()。isValid()的)
            继续;        浮动deltaTime =(System.nanoTime() - startTime时)/ 1000000000.0f;
        STARTTIME = System.nanoTime();        。game.getCurrentScreen()更新(deltaTime);
        game.getCurrentScreen()present(deltaTime)。        帆布帆布= holder.lockCanvas();
        canvas.getClipBounds(dstRect);
        canvas.drawBitmap(帧缓冲,空,dstRect,NULL);
        holder.unlockCanvasAndPost(画布);
    }
}

因此​​,重复调用更新在当前屏幕上,这是 LoadingScreen 的实例。

I am reading the book "Beginning Android Games" since last couple of days. But I am stuck to understand the code.

You can check or download the code here: http://code.google.com/p/beginnginandroidgames2/downloads/list

The project I mean is ch06-mr-mom. The Activity is called MrNomGame:

public abstract class AndroidGame extends Activity implements Game {

    AndroidFastRenderView renderView;
    Graphics graphics;
    Audio audio;
    Input input;
    FileIO fileIO;
    Screen screen;
    WakeLock wakeLock;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
        int frameBufferWidth = isLandscape ? 480 : 320;
        int frameBufferHeight = isLandscape ? 320 : 480;
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
                frameBufferHeight, Config.RGB_565);

        float scaleX = (float) frameBufferWidth
                / getWindowManager().getDefaultDisplay().getWidth();
        float scaleY = (float) frameBufferHeight
                / getWindowManager().getDefaultDisplay().getHeight();

        renderView = new AndroidFastRenderView(this, frameBuffer);
        graphics = new AndroidGraphics(getAssets(), frameBuffer);
        fileIO = new AndroidFileIO(this);
        audio = new AndroidAudio(this);
        input = new AndroidInput(this, renderView, scaleX, scaleY);
        screen = getStartScreen();
        setContentView(renderView);

        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
    }

    @Override
    public void onResume() {
        super.onResume();
        wakeLock.acquire();
        screen.resume();
        renderView.resume();
    }

    @Override
    public void onPause() {
        super.onPause();
        wakeLock.release();
        renderView.pause();
        screen.pause();

        if (isFinishing())
            screen.dispose();
    }

    public Input getInput() {
        return input;
    }

    public FileIO getFileIO() {
        return fileIO;
    }

    public Graphics getGraphics() {
        return graphics;
    }

    public Audio getAudio() {
        return audio;
    }

    public void setScreen(Screen screen) {
        if (screen == null)
            throw new IllegalArgumentException("Screen must not be null");

        this.screen.pause();
        this.screen.dispose();
        screen.resume();
        screen.update(0);
        this.screen = screen;
    }

    public Screen getCurrentScreen() {
        return screen;
    }
}

The class implements the interface Game :

package com.badlogic.androidgames.framework;

public interface Game {
    public Input getInput();
    public FileIO getFileIO();
    public Graphics getGraphics();
    public Audio getAudio();
    public void setScreen(Screen screen);
    public Screen getCurrentScreen();
    public Screen getStartScreen();
}

My first question is: I missed the implementation of the method getStartScreen() of the interface Game. Usually I have to implement all methods of an interface.

Anyway, now the onCreate is running. At this line:

screen = getStartScreen();

The program goes back to the origin class MrNomGame, where the method getStartScreen() gives an object of LoadingScreen to the variable screen. Class LoadingScreen:

public class LoadingScreen extends Screen {

    public LoadingScreen(Game game) {
        super(game);
    }

    public void update(float deltaTime) {
        Graphics g = game.getGraphics();
        Assets.background = g.newPixmap("background.png", PixmapFormat.RGB565);
        Assets.logo = g.newPixmap("logo.png", PixmapFormat.ARGB4444);
        Assets.mainMenu = g.newPixmap("mainmenu.png", PixmapFormat.ARGB4444);
        Assets.buttons = g.newPixmap("buttons.png", PixmapFormat.ARGB4444);
        Assets.help1 = g.newPixmap("help1.png", PixmapFormat.ARGB4444);
        Assets.help2 = g.newPixmap("help2.png", PixmapFormat.ARGB4444);
        Assets.help3 = g.newPixmap("help3.png", PixmapFormat.ARGB4444);
        Assets.numbers = g.newPixmap("numbers.png", PixmapFormat.ARGB4444);
        Assets.ready = g.newPixmap("ready.png", PixmapFormat.ARGB4444);
        Assets.pause = g.newPixmap("pausemenu.png", PixmapFormat.ARGB4444);
        Assets.gameOver = g.newPixmap("gameover.png", PixmapFormat.ARGB4444);
        Assets.headUp = g.newPixmap("headup.png", PixmapFormat.ARGB4444);
        Assets.headLeft = g.newPixmap("headleft.png", PixmapFormat.ARGB4444);
        Assets.headDown = g.newPixmap("headdown.png", PixmapFormat.ARGB4444);
        Assets.headRight = g.newPixmap("headright.png", PixmapFormat.ARGB4444);
        Assets.tail = g.newPixmap("tail.png", PixmapFormat.ARGB4444);
        Assets.stain1 = g.newPixmap("stain1.png", PixmapFormat.ARGB4444);
        Assets.stain2 = g.newPixmap("stain2.png", PixmapFormat.ARGB4444);
        Assets.stain3 = g.newPixmap("stain3.png", PixmapFormat.ARGB4444);
        Assets.click = game.getAudio().newSound("click.ogg");
        Assets.eat = game.getAudio().newSound("eat.ogg");
        Assets.bitten = game.getAudio().newSound("bitten.ogg");
        Settings.load(game.getFileIO());
        game.setScreen(new MainMenuScreen(game));
    }

    public void present(float deltaTime) {

    }

    public void pause() {

    }

    public void resume() {

    }

    public void dispose() {

    }
}

Class Screen:

public abstract class Screen {
    protected final Game game;

    public Screen(Game game) {
        this.game = game;
    }

    public abstract void update(float deltaTime);
    public abstract void present(float deltaTime);
    public abstract void pause();
    public abstract void resume();
    public abstract void dispose();
}

After a certain time, the onCreate() method is finished. But in the Debug-Mode the screen of my mobile phone is already black and dark. It seems to be the program is in a loop. I can press F6 as often as I want. But when I resume the Main-Menu will shown. If you look in the code you will see that the Main-Menu will call in the update() method of loadingScreen.

My big question is: What happens after the onCreate() method is finished and how the program get into the update() method of loadingScreen?

I know that is a lot of code. But it would be very useful for me to understand it.

解决方案

For your first question: AndroidGame is an abstract class. This means it doesn't have to implement all methods of Game, as long as one of the classes extending AndroidGame does this. As you can see from the code, this is done in MrNomGame:

public class MrNomGame extends AndroidGame {
    public Screen getStartScreen() {
        return new LoadingScreen(this); 
    }
} 

Now for your big question:

In onCreate, a renderer is created:

renderView = new AndroidFastRenderView(this, frameBuffer);

If you have a look at that class, you see that onResume creates a new thread that runs the following code:

public void run() {
    Rect dstRect = new Rect();
    long startTime = System.nanoTime();
    while(running) {  
        if(!holder.getSurface().isValid())
            continue;           

        float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f;
        startTime = System.nanoTime();

        game.getCurrentScreen().update(deltaTime);
        game.getCurrentScreen().present(deltaTime);

        Canvas canvas = holder.lockCanvas();
        canvas.getClipBounds(dstRect);
        canvas.drawBitmap(framebuffer, null, dstRect, null);
        holder.unlockCanvasAndPost(canvas);
    }
}

So, it repetitively calls update on the current screen, which is the instance of LoadingScreen.

这篇关于我需要的&QUOT code进行解释;在开始的Andr​​oid游戏&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆