我正在按照我在http://www.androidauthority.com/android-game-java-785331/
找到的教程我只想要一个基本的教程让我开始在Android上制作游戏。 这是MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
}
}
以下是Android Studio声称为抽象的类:
public class MainThread extends Thread {
private SurfaceHolder surfaceHolder;
private GameView gameView;
private boolean running;
public static Canvas canvas;
public MainThread(SurfaceHolder surfaceHolder, GameView gameView) {
super();
this.surfaceHolder = surfaceHolder;
this.gameView = gameView;
}
@Override
public void run() {
while (running) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized(surfaceHolder) {
this.gameView.update();
this.gameView.draw(canvas);
}
} catch (Exception e) {} finally {
if (canvas != null) {
try {
surfaceHolder.unlockCanvasAndPost(canvas);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public void setRunning(boolean isRunning) {
running = isRunning;
}
}
以下是试图实例化“抽象”类的类:
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
public MainThread thread;
public GameView(Context context) {
super(context);
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
thread.setRunning(false);
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
retry = false;
}
}
public void update() {
}
}
我复制并粘贴了教程中的所有代码。我唯一能想到的是超级课程?或者如果有一个按钮我在创建使其抽象的类时意外地单击了?我不确定是什么问题,但我不能继续使用错误的教程。如果有人可以看到什么是错的,或者可以推荐另一个Android Studio游戏教程,我将非常感激。
答案 0 :(得分:0)
这是命名问题。我遇到了完全相同的问题,只是将其更改为“ GameThread”即可解决问题