【问题标题】:Get score value from serializable class to activity从可序列化的类中获取分值到活动
【发布时间】:2017-01-30 15:16:48
【问题描述】:

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

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);
    }
}

【问题讨论】:

  • 在 Game 类中创建返回分数的方法
  • @pcpriyanka 我按照某人的建议在最后一天尝试了,但是当分数达到 1 时没有任何显示
  • Game 与 MainActivity 的关系,是 MainActivity.class 的字段还是他们只是通过消息“聊天”。简而言之,能否提供 MainActivity 的代码?
  • @MichaelSpitsin 我刚刚编辑过请检查:)
  • @ArslanAli Okey。下一个问题是你如何与 Game 交互。你是使用直接方法调用,还是游戏在另一个线程或服务中运行?

标签: android android-activity serialization


【解决方案1】:
public static void setScore(int s) {
score=s;
}

public static int getScore()
{
return score;
}

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

int scr=Game.getScore(); 

上面的代码会返回分数

【讨论】:

  • 好吧,让我试试这个。
  • 上述方法写入Game Calss
  • setScore(int s) .. 在该方法中写入 Log.e("value",s);查看该值是否正在更新
  • 没有错误,但没有 toast 或 log.e 当分数达到大于 1 时显示我将 getter 和 setter 放入游戏类并将此代码写入我的活动 int scr=Game.getScore(); if (scr > 1) { Log.e("value", "one"); }
【解决方案2】:

创建一个界面

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    相关资源
    最近更新 更多