0

我猜是一个基本的Java问题。我正在阅读一个教程,其中我有一个自定义类“LoadingScreen”(扩展了另一个自定义类“Screen”)。

一旦调用“LoadingScreen”类,“update()”方法就会运行。

问题是从哪里调用 update() 方法?在 Java 中使用构造函数调用 update() 方法是默认的吗?

public class LoadingScreen extends Screen {

public LoadingScreen(Game game) { //constructor for this class
    super(game);
}

@Override
public void update(float deltaTime) {
    Graphics g = game.getGraphics();
    Assets.background = g.newPixmap("background.png", Graphics.PixmapFormat.RGB565);
    Assets.logo = g.newPixmap("logo.png", Graphics.PixmapFormat.ARGB4444);
    Assets.mainMenu = g.newPixmap("mainmenu.png", Graphics.PixmapFormat.ARGB4444);
    Assets.buttons = g.newPixmap("buttons.png", Graphics.PixmapFormat.ARGB4444);
    Assets.help1 = g.newPixmap("help1.png", Graphics.PixmapFormat.ARGB4444);
    Assets.help2 = g.newPixmap("help2.png", Graphics.PixmapFormat.ARGB4444);
    Assets.help3 = g.newPixmap("help3.png", Graphics.PixmapFormat.ARGB4444);
    Assets.numbers = g.newPixmap("numbers.png", Graphics.PixmapFormat.ARGB4444);
    Assets.ready = g.newPixmap("ready.png", Graphics.PixmapFormat.ARGB4444);
    Assets.pause = g.newPixmap("pausemenu.png", Graphics.PixmapFormat.ARGB4444);
    Assets.gameOver = g.newPixmap("gameover.png", Graphics.PixmapFormat.ARGB4444);
    Assets.headUp = g.newPixmap("headup.png", Graphics.PixmapFormat.ARGB4444);
    Assets.headLeft = g.newPixmap("headleft.png", Graphics.PixmapFormat.ARGB4444);
    Assets.headDown = g.newPixmap("headdown.png", Graphics.PixmapFormat.ARGB4444);
    Assets.headRight = g.newPixmap("headright.png", Graphics.PixmapFormat.ARGB4444);
    Assets.tail = g.newPixmap("tail.png", Graphics.PixmapFormat.ARGB4444);
    Assets.stain1 = g.newPixmap("stain1.png", Graphics.PixmapFormat.ARGB4444);
    Assets.stain2 = g.newPixmap("stain2.png", Graphics.PixmapFormat.ARGB4444);
    Assets.stain3 = g.newPixmap("stain3.png", Graphics.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));
}

@Override
public void present(float deltaTime) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {

}
}

更新 1:添加“屏幕”类

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

更新 2:添加“游戏”界面

包 com.neil.gamesbook.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();
}
4

2 回答 2

0

不,构造函数默认不调用任何方法。

当你说“一旦调用“LoadingScreen”类,“update()”方法就会运行。'是什么意思?您的意思是创建 LoadingScreen 类的实例吗?

于 2013-10-20T02:22:33.693 回答
0

在 Java 中使用构造函数调用 update() 方法是默认的吗?

不,构造函数默认不调用任何东西。

问题是从哪里调用 update() 方法?

您的构造函数在这里调用超类的构造函数。所以可能update()是从那里调用的。您应该查看类的构造函数Screen

编辑
书中的那句话是不正确的。似乎 AndroidGame 构造函数间接调用了 update()。看到这个:我需要解释“开始安卓游戏”的代码

于 2013-10-20T02:25:31.293 回答