我猜是一个基本的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();
}