我读了写在“Beginning Android Games”一书中的代码,但我并不完全理解它。该应用程序的入口点是:
public class MrNomGame extends AndroidGame {
public Screen getStartScreen() {
return new LoadingScreen(this);
}
}
在这里扩展的“AndroidGame”类:
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;
}
}
您会看到类本身扩展了“活动”。这意味着如果您在手机上打开该应用程序,该应用程序就会进入。在 onCreate 方法中有一个称为“getStartScreen()”的方法。那是实现的接口“游戏”的方法。但是方法 'getStartScreen()' 没有在 'AndroidGame' 中实现,因此它是一个抽象类。正如你所记得的,方法 'getStartScreen()' 在 'MrNomGame' 中实现。正如我已经提到的,方法“getStartScreen()”在 onCreate 方法中被调用。这就是让我感到困惑的一点。超类从其子类调用方法。它是如何工作的?如果有两个扩展“AndroidGame”的子类会发生什么?例如:
public class MrNomGame2 extends AndroidGame {
public Screen getStartScreen() {
return new LoadingScreen(this);
}
}
将调用哪个“getStartScreen()”方法?来自 MrNomGame 的那个还是来自 MrNomGame2 的那个?
至少游戏界面:
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();
}
我知道那是很多代码。我希望你能帮助我。非常感谢。
彼得