我正在开发一个游戏,其中有一个可序列化的类,它有一个分数参数,并且最初设置为 0。 现在我想要的是,当分数大于 1 时,分数值将传递给我的主要 Activity ,并且一些 toast 会显示在主要 Activity 中 这是我的可序列化类的代码:
public class Game implements Serializable{
private static final long serialVersionUID = 8326065952389292265L;
private int score = 0;
这是我的分数增加,当它大于 1 时,分数值应该传递给主要 Activity
if(bird.GetX()+bW > px1 && bird.GetX() < px2)
{
if(birdY1>=minY && birdY2<=maxY)
{
}
else
{
if(!boom)
SoundManager.playSound(5, 1);
boom = true;
bird.SetState(0);
}
score = (i+1);
if (score>1)
{
}
}
我很困惑如何将此分数值发送到我的主要 Activity ,然后如何在主要 Activity 中获取它。谁能帮我一些小代码来说明它是如何完成的?任何帮助将不胜感激
这是我的 MainActivity 代码:
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private SurfaceView mainGameView;
static Bitmap bitmap;
static Canvas canvas;
private GameLogic gameLogic;
private Game gamescore;
private ArrayList<String> wordsDictionary;
private Context context;
private MyTask mt;
private boolean dictionaryLoaded;
private ImageView image;
private Activity activity;
Intent playbackServiceIntent;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
SoundManager.getInstance();
SoundManager.initSounds(this);
SoundManager.loadSounds();
SoundManager.playSound(1, 1);
if(true)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
else
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
context = this;
gameLogic = new GameLogic(context, getResources());
dictionaryLoaded = false;
SharedPreferences sPref = getPreferences(MODE_PRIVATE);
String data1 = sPref.getString("data1", "");
mainGameView = new MainGameView(this, gameLogic);
final RelativeLayout layout = new RelativeLayout(context);
layout.addView(mainGameView);
Runnable runnable = new Runnable() {
@Override
public void run() {
}
};
new Thread(runnable).start();
if(true)
{
setContentView(layout);
}
}
最佳答案
public static void setScore(int s) {
score=s;
}
public static int getScore()
{
return score;
}
现在您可以在主要 Activity 中使用 as :
int scr=Game.getScore();
上面的代码将返回分数
关于android - 获取从可序列化类到 Activity 的分数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39632353/