从可序列化类到活动获取分数值

时间:2016-09-22 07:02:45

标签: android android-activity serialization

我正在开发一款游戏,其中我有一个可序列化的类,它有一个得分参数,最初设置为0。 现在我想要的是当分数大于1时,分数值将传递给我的主要活动,并且主要活动中会显示一些吐司 这是可序列化类的代码:

public class Game implements Serializable{
private static final long serialVersionUID = 8326065952389292265L;
private int score = 0;

这是我的分数增加,当它大于1时,分数值应该传递给主要活动

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)
                {

                }

            }

我很困惑如何将此分数值发送到我的主要活动,然后如何在主要活动中获取它。任何人都可以帮我一些小代码,如何完成它?任何帮助将不胜感激

这是我的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);
    }
}

2 个答案:

答案 0 :(得分:2)

public static void setScore(int s) {
score=s;
}

public static int getScore()
{
return score;
}

现在您可以在主Activity中使用as:

int scr=Game.getScore(); 

上面的代码将返回分数

答案 1 :(得分:0)

创建一个界面

 public interface GameScoreListener{
  void onScoreIncrease(int incrementBy);
  void onScoreDecrease(int decrementBy)
}

GameScoreListener中实施MainActivity。 在您改变分数的班级中创建GameScoreListener setter和getter。当任何方法中的分数发生变化时,请检查GameScoreListener的NPE,然后从GameScoreListener进行回调。