gpt4 book ai didi

java - 从 SurfaceView 创建类时出错,该类将作为线程运行

转载 作者:行者123 更新时间:2023-12-01 10:34:15 25 4
gpt4 key购买 nike

我了解 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com