gpt4 book ai didi

java - 以类似于 Doodle Jump 和 Cut the Rope 的方式增加分数

转载 作者:行者123 更新时间:2023-11-30 03:08:00 25 4
gpt4 key购买 nike

在 Cut the Rope 的关卡清除屏幕中,分数会递增到您在该关卡中获得的分数。我怎么能做这样的事情?我尝试使用 for 循环,一旦我达到该值就停止递增,但这会使我的应用程序崩溃。我想它需要在每个增量上设置 textview 的文本。有什么想法吗?

目前这是我在 Game Over 中显示分数的方式:

// get score and set text field
scorePreferences = getSharedPreferences("score", 0);
score = scorePreferences.getInt("score", 0);
scoreValue.setText(Integer.toString(score));

我希望它增加到首选项中保存的值。我试着这样做:

for (int i = 0; i < score; i++) {
scoreValue.setText(i);
}

记录猫消息:

01-30 14:11:19.606: E/AndroidRuntime(26279): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.main.francois/com.main.francois.GameOverActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.os.Handler.dispatchMessage(Handler.java:102)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.os.Looper.loop(Looper.java:136)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-30 14:11:19.606: E/AndroidRuntime(26279): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 14:11:19.606: E/AndroidRuntime(26279): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-30 14:11:19.606: E/AndroidRuntime(26279): at dalvik.system.NativeStart.main(Native Method)
01-30 14:11:19.606: E/AndroidRuntime(26279): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.content.res.Resources.getText(Resources.java:244)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.widget.TextView.setText(TextView.java:3888)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.main.francois.GameOverActivity.incrementScore(GameOverActivity.java:135)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.main.francois.GameOverActivity.load(GameOverActivity.java:130)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.main.francois.GameOverActivity.onCreate(GameOverActivity.java:78)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.Activity.performCreate(Activity.java:5231)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

编辑:我写了我的计时器来计算分数,如下所示:

public void scoreTimer() {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (incrementScore < score) {
incrementScore++;
scoreValue.setText(String.valueOf(incrementScore));
}
}
}, delay, period);
}

但我现在得到这个错误:

01-30 14:57:55.006: E/AndroidRuntime(1383): Process: com.main.francois, PID: 1383
01-30 14:57:55.006: E/AndroidRuntime(1383): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.View.invalidate(View.java:10935)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.View.invalidate(View.java:10890)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.widget.TextView.checkForRelayout(TextView.java:6587)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.widget.TextView.setText(TextView.java:3813)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.widget.TextView.setText(TextView.java:3671)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.widget.TextView.setText(TextView.java:3646)
01-30 14:57:55.006: E/AndroidRuntime(1383): at com.main.francois.GameOverActivity$3.run(GameOverActivity.java:145)
01-30 14:57:55.006: E/AndroidRuntime(1383): at java.util.Timer$TimerImpl.run(Timer.java:284)

我猜我需要使用“异步”或“runonuithread”方法?但我不确定该怎么做?

最佳答案

将分数设置为 String

for (int i = 0; i < score; i++) {
scoreValue.setText(String.valueOf(i));
}

实际上,当您将一个 int 传递给 setText() 方法时,它会将其作为资源 ID 并尝试在字符串资源(写在 res/string.xml 中)中找出字符串。这就是您遇到 ResourceNotFoundException 的原因:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.main.francois/com.main.francois.GameOverActivity}:
android.content.res.Resources$NotFoundException: String resource ID #0x0

关于java - 以类似于 Doodle Jump 和 Cut the Rope 的方式增加分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21459277/

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