我知道OOP的基本概念,我正在尝试使用Android编程(尤其是游戏)。我正在尝试通过示例来实现本书Android游戏编程中的一个游戏项目。我能够从理论上理解所说的话。但是当我将这些代码放到一个单独的java类TDView.java文件中时,我在Android studio中遇到了一个错误。也许我误解了一些短语,并把一篇文章放在了我没有想到的地方。从SurfaceView创建一个类时出错,该线程将作为线程运行
的错误标记ERROR1和误差2在下面的代码:
package com.gamecodeschool.c1tappydefender;
import android.content.Context;
import android.view.SurfaceView;
public class TDView extends SurfaceView implements Runnable {
volatile boolean playing;
Thread gameThread = null;
@Override
public void run() {
while (playing) {
update();
draw();
control();
} //error2: it says a semicolon is needed here.
private void update(){
}
private void draw(){
}
private void control(){
}
}// error1: it says class or interface missing
public TDView(Context context) {
super(context);
}
// Clean up our thread if the game is interrupted or the player quits
public void pause() {
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
}
}
// Make a new thread and start it
// Execution moves to our R
public void resume() {
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
}
移动更新,绘制和控制方法之外的运行方法,所以他们实际上是类的方法。 –
所以基本上运行是一种实现(调用)其他三个方法? – Akash
运行方法调用其他三个是。您不允许在当前拥有它们的地方声明方法。这是错误来自的地方。 –