我正在编写一个 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)