吉格尔不会攻击树木生命 [英] Gigel does not attack tree life

查看:62
本文介绍了吉格尔不会攻击树木生命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        Sprite gigel = new Sprite(WindowsFormsApplication2.Properties.Resources.Gigel_img);
        Sprite tree = new Sprite(WindowsFormsApplication2.Properties.Resources.tree_img);

        void Screen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.E)
            {
                gigel.Atack(tree.life);
                tree.LifeUpdate();
            }
            gigel.Reposition();
            Refresh();
        }

...
inside Sprite class 
        public void Atack(int targetLife)
        {
            targetLife -= damage;
        }







'targetLife'正在减少符合'损害'

但是没有将值OUT传递给tree.life来自gigel.Atack(tree.life);

tree.life没有从流程中获得价值!为什么?或者我做错了什么?

谢谢。



我尝试了什么:






'targetLife' is decrementing conform 'damage'
but is not passing the value OUT to tree.life from "gigel.Atack(tree.life);"
tree.life is not getting the value from process ! Why? Or what i am doing wrong?
Thank you.

What I have tried:

//I hope now is more clear: 

            if (e.KeyCode == Keys.E)
            {
                int num = 20;
                gigel.Atack(num);
                tree.life = num; // num is 20 and not 7

                tree.LifeUpdate();
            }
...
inside Sprite class 
        public void Atack(int targetLife) //targetLife = 20
        {
            targetLife -= damage; // 20-13 targetLife=7
            //from this point (num) must take targetLife value of 7
            //but (num) remains at 20.
        }

推荐答案

所以,这是基本问题:值类型与引用类型。有关详细信息,请阅读:



C#中的值与参考类型 [ ^ ]



当您传递值类型(例如 int )时,例如 num tree.life 作为参数,您传递的是该值的副本。在您的方法( Attack )中,您将递减该值的副本 targetlife ),不是原件。当您从 Attack 方法返回时,该副本( targetlife )超出范围并被丢弃。



在现代版本的C#中,可以通过引用传递值类型,使用 ref out 关键字。有关详细信息,请参阅以下链接:



ref(C#参考)| Microsoft Docs [ ^ ]

out参数修饰符(C#参考)| Microsoft Docs [ ^ ]



但是,在您的情况下,可能还有其他注意事项。虽然源代码未共享,但我怀疑 life 可能是一个属性。遗憾的是,目前无法使用 ref / out 关键字传递房产。



所以,我会考虑稍微重新建模你的 Sprite 类,这对于 gigel来说都很常见。在您的代码中,似乎 gigel 正在攻击,因此攻击的方法签名应该更接近这个动作。



我建议你改变攻击的方法签名如下:

So, here is the basic problem: value type versus reference type. For more details, read:

Value vs Reference Types in C#[^]

When you pass a value type (e.g. int) like num or tree.life as a parameter, you are passing a copy of that value. In your method (Attack), you are then decrementing the copy of the value (targetlife), not the original. When you return from the Attack method, that copy (targetlife) passes out of scope and is discarded.

In modern versions of C#, it is possible to pass a value type, by reference, using the ref or out keyword. For details, see the following links:

ref (C# Reference) | Microsoft Docs[^]
out parameter modifier (C# Reference) | Microsoft Docs[^]

However, in your case, there may be additional considerations. While the source code is not shared, I suspect life may be a property. Regrettably, it is not currently possible to pass a property using the ref/out keyword.

So, I would consider slightly re-modeling your Sprite class, which is common to both gigel and tree. In your code, it seems that gigel is attacking tree, so the method signature for Attack should probably more closely resemble this action.

I would suggest you change the method signature of Attack as follows:
public void Attack(Sprite target) =>
  target.life -= damage;

然后,让 gigel 攻击,你会做以下事情:

Then, to have a gigel attack a tree, you would do the following:

gigel.Attack(tree);


这篇关于吉格尔不会攻击树木生命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆