gpt4 book ai didi

java - 打地鼠 - 线程中的 setButton

转载 作者:行者123 更新时间:2023-12-02 12:45:03 26 4
gpt4 key购买 nike

我制作了一款类似于打地鼠的游戏。你必须点击“x”而不是痣。我想随机更改“x”的位置,但出现以下错误“错误:找不到符号方法 getButton1()”(zufall 在德语中是随机的意思)

线程 zufallsXThread = new Thread() {

        @Override
public void run() {
while (fail > 3) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}


runOnUiThread(new Runnable() {
@Override
public void run() {
int zufall = (int) (Math.random() * 9) + 1;

if (zufall == 1) {
this.getButton1().setText("x");
} else if (zufall == 2) {
this.getButton2().post("x");
} else if (zufall == 3) {
this.getButton3().setText("x");
} else if (zufall == 4) {
this.getButton4().setText("x");
} else if (zufall == 5) {
this.getButton5().setText("x");
} else if (zufall == 6) {
this.getButton6().setText("x");
} else if (zufall == 7) {
this.getButton7().setText("x");
} else if (zufall == 8) {
this.getButton8().setText("x");
} else if (zufall == 9) {
this.getButton9().setText("x");
}

}
});

}
}
};

最佳答案

从调用方法中删除 this 关键字。

if (zufall == 1) {
getButton1().setText("x");
}

并更改 run 方法内的所有内容。

关于java - 打地鼠 - 线程中的 setButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44805292/

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