Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过引用将Bbject传递给类_C#_Xna - Fatal编程技术网

C# 通过引用将Bbject传递给类

C# 通过引用将Bbject传递给类,c#,xna,C#,Xna,我把飞船的参数作为对象传递给“shotmanager”类,但问题是飞船的参数是固定的,不会改变,即使我改变了飞船的“位置”之类的东西 以下是shotmanager的构造函数: shotmanager = new Shotmanager(shottexture, graphics.GraphicsDevice.Viewport.Bounds, ref spaceship, enemyship); 这里是我使用“宇宙飞船”的地方 我一直在改变位置,但很明显,物体不是通过参照发送的,换句话说,宇宙飞

我把飞船的参数作为对象传递给“shotmanager”类,但问题是飞船的参数是固定的,不会改变,即使我改变了飞船的“位置”之类的东西

以下是shotmanager的构造函数:

shotmanager = new Shotmanager(shottexture, graphics.GraphicsDevice.Viewport.Bounds, ref spaceship, enemyship);
这里是我使用“宇宙飞船”的地方

我一直在改变位置,但很明显,物体不是通过参照发送的,换句话说,宇宙飞船的初始值是发送的


我是一个初学者程序员,请不要驱逐我

您的
shipclass
是值类型吗?(也就是说,类定义写得像
public struct shipclass
?)我假设您正在更改
Fireshot
方法中的位置,您可以发布
Fireshot
方法吗?如果我不得不猜测的话,你“改变飞船位置”的方式是你只是改变了传递到
Fireshot
的位置值,但这很可能是坐标的副本;您可能需要将新值重新分配到船舶本身。感谢您的快速回复。对于A)您的船舶等级值类型是什么?我为spaceship对象声明了一个类shipclass。B) @ChrisSinclair B)你能发布射击方法吗?私有void Fireshot(Vector2 spaceshipposition,Vector2 spaceshipshotvelocity){var shotposition=spaceshipposition;var shot=new shot(shottexture,shotposition,Movementbounds);shots.Add(shot);}C)Position是shipclass中的2d向量,问题是当我改变目标飞船的位置时,传递给firemanager的变量位置没有改变。我想我必须通过参考来传递它。我不能用一种合适的方式来编排文本。如果你能提出建议,我将不胜感激。
    public Shotmanager(Texture2D shottexture, Rectangle Movementbounds, ref shipclass spaceship, EnemyManager enemyship)
    {
        // TODO: Complete member initialization
        this.shottexture = shottexture;
        this.Movementbounds = Movementbounds;
        this.spaceship = spaceship;
        this.enemyship =  enemyship;
    } 
            {
                Fireshot( spaceship.position, spaceshipshotvelocity);
            }