从之前的两场比赛创建基于记忆的游戏

时间:2018-05-08 16:56:45

标签: android android-intent

我正在开发一套游戏,以帮助有缺陷的孩子更快地学习:第一个游戏包含一组颜色,用户必须从一组四个中选择正确的游戏;第二个播放声音,用户必须选择发出声音的动物/车辆。 每个游戏包含6个难度级别。对于每个级别,用户有3次机会选择正确的答案。游戏一个接一个地播放(如果用户在第一个游戏中无法通过一个级别,它将继续第二个游戏)。

对于第三场比赛,我想制作前两场比赛的记忆游戏。假设用户没有在第一个游戏中通过第2级,在第二个游戏中没有通过第3级。然后第三个游戏将有2个Intent:第一个游戏将从他通过的第一个游戏中的级别中随机选择(在这种情况下它将是第一个或第二个级别),第二个游戏将从第1级, 2或3 - 在这种情况下 - 来自第二场比赛。

我想在用户未通过并在每个游戏中通过Intent发送一个整数并使用该数字为第三个游戏选择一个级别。 到目前为止,我尝试使用这个: 这是第一场比赛:

if(contor2 == 3) //the user has no more chances left
    {
        Intent intent1 = new Intent(FirstGame2.this, ThirdGame.class);
        intent1.putExtra("var1", 2); //the value of the current level

        Intent intent = new Intent(FirstGame2.this, SecondGame1.class);
        startActivity(intent); //start the second game
    }

在第二场比赛中:

if(contor == 3) //the user is out of chances
    {
        Intent intent1 = new Intent(SecondGame3.this, ThirdGame.class);
        intent1.putExtra("var2", 3); //in the second game he didn't pass level 3, so we're sending the value "3"
        startActivity(intent1); //start the third game        
    }

在第三场比赛中,我有以下代码:

    Intent intent = getIntent();
    final int intValue = intent.getIntExtra("var1", 0);

    Intent intent2 = getIntent();
    final int intValue2 = intent2.getIntExtra("var2", 0);

    Toast.makeText(getApplicationContext(),"The value of var1 is "+ intValue, Toast.LENGTH_LONG).show();

但它返回“0”而不是“2”,所以我猜数据毕竟不会发送....

关于如何实施第三款游戏的任何其他想法?非常感谢你!

1 个答案:

答案 0 :(得分:0)

在第一个游戏中,您将额外内容添加到 intent1 ,但是您使用 intent 开始活动。所以,你没有使用这部分代码:

     Intent intent1 = new Intent(FirstGame2.this, ThirdGame.class);
     intent1.putExtra("var1", 2); //the value of the current 

这就是你看到默认值的原因:0。

如果你想传递一些东西,你可以直接进入第三场比赛,或者你将var1传递给第二场比赛,你收集它并再次发送到第三场比赛。例如:

intent1.putExtra("var2", 3);
intent1.putExtra("var1", 2); or intent1.putExtra("var1", getIntent().getIntExtra("var2", 0));
相关问题