如何在c#中写“如果玩家获得+1”

时间:2015-10-28 19:03:06

标签: c# visual-studio-2015

所以我添加的代码就是,当'玩家'获得+ 1'黄金'时(你可以获得金币来杀死怪物,例如每5-10个怪物)

我不知道怎么说它如果玩家获得+ 1金币,我所拥有的只是

    private void GoldNotification(int Gold)
    {
        Gold = Client.Player.Gold;

        if (Gold != -1 && Client.Player.Gold != Gold)
        //then here im going to add code for it to display a notification that says +1 Gold above your character for a brief few seconds just so people know when they gain gold
    }

只关注

    if (Gold != -1 && Client.Player.Gold != Gold)

因为那些我真正质疑,我在VS中得到0个错误,但是当我在游戏中测试它时,它不起作用。

2 个答案:

答案 0 :(得分:1)

您必须在检查前保留原始值,然后将黄金添加到播放器。

private void GoldNotification(int Gold)
{
    //Gold = Client.Player.Gold; // remove this

    if (Gold != -1 && Client.Player.Gold != Gold + Client.Player.Gold) // after this check add gold to player
    {
        Client.Player.Gold += Gold;
        //...
    }
}

此部分Client.Player.Gold != Gold + Client.Player.Gold如果黄金为0,则条件变为假意味着没有添加黄金

编辑:我太蠢了。这可以简化为

if (Gold > 0)

这意味着黄金大于0,所以我们确定将其添加到玩家。

答案 1 :(得分:0)

据我了解你的问题,你试图在他/她获得金牌时通知用户。现在假设您将Gold的新值传递给函数,您的代码应该是:

private void GoldNotification(int newGold)
{
    var oldGold = Client.Player.Gold;

    if (newGold != -1 && newGold > oldGold) 
    //they've got gold. prceed to notify
}

但是如果您希望流程自动化,您可以使用事件。您可以将Gold作为属性实现,并在其值发生变化时触发事件。

同样,我并不是100%确定你到底想要达到的目的,但希望这会有所帮助。