Xamarin中的意图没有传递正确的值

时间:2017-01-08 22:23:27

标签: c# android visual-studio android-intent xamarin

我目前正在使用Visual Studio中的Xamarin开发一款针对Android的小游戏,我有游戏玩法和屏幕游戏。在屏幕上的游戏中,我希望显示用户得分,这是我从意图中获得的。这是第一次完美运行,但如果用户选择重新启动游戏,无论他们得分如何,屏幕上的游戏都会将他们的得分默认为零。我该如何解决这个问题,以便每次都显示正确的分数?此外,我还是比较陌生,所以任何帮助都会很棒!

游戏课程中的代码通过屏幕调用游戏

this.intent = new Intent(Android.App.Application.Context, typeof(GameOverAct));
                this.intent.PutExtra("ThePoint", buttons.score);
                this.intent.SetFlags(ActivityFlags.NewTask);
                Android.App.Application.Context.StartActivity(intent);

游戏结束活动

public class GameOverAct : Microsoft.Xna.Framework.AndroidGameActivity
{
    TextView score;
    protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);
        SetContentView(Resource.Layout.layout1);
        score = FindViewById<TextView>(Resource.Id.scoretext);
        score.Text = "Score: " + getGameScore();
        removescore();

        Button restart = FindViewById<Button>(Resource.Id.restart);
        restart.Click += (sender, e) =>
        {
            var intent = new Intent(this, typeof(Activity1));
            StartActivity(intent);
        };
        Button mm = FindViewById<Button>(Resource.Id.mainmenu);
        mm.Click += (sender, e) =>
        {
            var intent3 = new Intent(this, typeof(MainMenuAct));
            StartActivity(intent3);
        };
    }
    protected override void OnRestart()
    {
        base.OnRestart();
        SetContentView(Resource.Layout.layout1);
        score = FindViewById<TextView>(Resource.Id.scoretext);
        score.Text = "Score: " + getGameScore();
        removescore();
        Button restart = FindViewById<Button>(Resource.Id.restart);
        restart.Click += (sender, e) =>
        {
            var intent = new Intent(this, typeof(Activity1));
            StartActivity(intent);
        };
        Button mm = FindViewById<Button>(Resource.Id.mainmenu);
        mm.Click += (sender, e) =>
        {
            var intent3 = new Intent(this, typeof(MainMenuAct));
            StartActivity(intent3);
        };
    }
    public int getGameScore()
    {
        int a = Intent.GetIntExtra("ThePoint", 0);
        return a;
    }
    public void removescore()
    {
        Intent.RemoveExtra("ThePoint");
    }
 }

运行游戏类的活动

public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
    Game1 g;

    //When the Game is played for the first time after launching the app, this method is called
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        //Gets a Game1 object, which is the class that builds the game
        this.g = new Game1();
        //sets it to view
        SetContentView((View)g.Services.GetService(typeof(View)));
        g.Run();


    }
    //If the player chooses to restart, this method is called
    protected override void OnRestart()
    {
        base.OnRestart();
        this.g = new Game1();
        SetContentView((View)g.Services.GetService(typeof(View)));
        g.Run();

    }
}

0 个答案:

没有答案