我了解 OOP 的基本概念,我正在尝试 Android 编程。(特别是游戏)。我正在尝试通过示例实现本书《Android 游戏编程》中的游戏项目。我能够从理论上理解所说的内容。但是,当我将这些代码 fragment 组合到一个 java 类 TDView.java 文件中时,我在 Android studio 中收到错误。也许我误解了一些短语并把一 block 放在了我不应该放的地方。
以下代码中的错误标记为 error1 和 error2:
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();
}
}
最佳答案
将方法声明移到运行方法的声明之外。这应该可以解决问题
public void run() {
while (playing) {
update();
draw();
control();
}
}
private void update(){
}
private void draw(){
}
private void control(){
}
关于java - 从 SurfaceView 创建类时出错,该类将作为线程运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34883187/