1

我正在编写一个 android 游戏,其中精灵从屏幕上下来,用户点击它们以删除它们。当精灵到达屏幕底部时,玩家的生命减少并且精灵被移除,但显示玩家生命​​的文本视图不会更新。

我在用于杀死精灵的 on touch 监听器中放置了一个“更新”调用,这会更新一切正常(当精灵被杀死时,玩家得分会增加)。但是我不知道当精灵到达屏幕底部时如何更新生命。每次我尝试在删除精灵时更新它时,应用程序都会崩溃。我正在使用 (TextView).setText() 来更新显示分数的文本视图

该应用程序使用 openGL 来渲染精灵。

删除精灵时如何使其更新?

这是设置屏幕的类,它也有触摸监听器。

package com.JACSoft.game;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GamePlay extends Activity 
{
    public static GameGLSurfaceView mGLView;
    public static int PlayerScore = 0;
    public static int PlayerLives = 9;
    public static int counter = 0;
    static TextView PStatsView;         // creates text view to display stats
    static LinearLayout Stats;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Stats = new LinearLayout(this);             //creates an editable layout
    Stats.setOrientation(1);                                // makes it horizontal
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    mGLView = new GameGLSurfaceView(this);
    PStatsView = new TextView(this);
    PStatsView.setTextSize(20.0f);
    PStatsView.setText("Score : " + PlayerScore + "             " + "Lives : " + PlayerLives);          // puts the string version of score in the text view 
    Stats.addView(PStatsView);                              // adds views to layout
    Stats.addView(mGLView);                                 // adds views to layout
    setContentView(Stats);                                  // sets layout as the view
}

public boolean onTouchEvent(MotionEvent event) 
{
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN :
            GameGLRenderer.sprites.deleteSprite(event.getX(),event.getY(), 0);
            PStatsView.setText("Score : " + PlayerScore + "             " + "Lives : " + PlayerLives);          
            break;
    }
    return true;
}

}

这是说我无法从其他线程访问文本视图的位,但我尝试从主线程中的精灵删除线程调用一个函数,然后调用 setText 函数

11-22 19:06:03.105: E/AndroidRuntime(27971): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

这是该行之后的内容

11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4865)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:979)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.view.ViewGroup.invalidateChild(ViewGroup.java:4306)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.view.View.invalidate(View.java:10519)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.view.View.invalidate(View.java:10474)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.widget.TextView.checkForRelayout(TextView.java:6598)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.widget.TextView.setText(TextView.java:3705)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.widget.TextView.setText(TextView.java:3563)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.widget.TextView.setText(TextView.java:3538)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at com.JACSoft.game.GameSprites.deleteSprite(GameSprites.java:120)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at com.JACSoft.game.GameSprite.BottomReached(GameSprite.java:61)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at com.JACSoft.game.GameSprite.moveBallDown(GameSprite.java:51)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at com.JACSoft.game.GameSprite.gravitate(GameSprite.java:25)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at com.JACSoft.game.GameSprites.draw(GameSprites.java:60)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at com.JACSoft.game.GameGLRenderer.onDrawFrame(GameGLRenderer.java:68)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
11-22 19:06:03.105: E/AndroidRuntime(27971):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
4

1 回答 1

1

您需要在每个游戏循环中迭代精灵并检查是否有任何精灵到达屏幕底部,然后将其删除并更新玩家统计信息。

许多游戏框架都有一个单独的线程来绘制和更新游戏对象。您的问题可能是该setText方法是从该线程调用的,而不是从 UI 线程调用的。将此方法添加到您的活动类:

public void setPStatsText(String text) {
    this.runOnUiThread(new Runnable() {
        public void run() {
            PStatsView.setText(text);
        }
    }
}

并调用它来更改文本。

如果这没有帮助,请从 logcat 发布异常消息。

于 2012-11-22T18:16:18.953 回答