分配指的是错误的对象

时间:2014-12-18 21:02:00

标签: c# monogame assign

我正在使用制作游戏并尝试使用控制器管理器来管理用户输入。

我已经创建了一个类ControllerManager来执行此操作。因为我尝试从2个控制器获取输入,所以我创建了三个ControllerManagercontrollerManagercontroller1Managercontroller2Manager的实例。

现在,在我的播放器对象中,我有一个局部变量localManager,我需要将其分配给controller1Managercontroller2Manager,具体取决于它是哪个播放器。

我一直在尝试分配它:

this.localManager = Controller1Manager;

这会导致localManager变量设置为controllerManager

所以有人看到我做错了吗?我不知道除了它可能与指针/引用/单例有关但我已经检查了组件列表并且每个经理都在那里。

编辑: 根据要求,这里是构造函数,类创建以及我尝试更改值的位置

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using GDApp;

namespace GDLibrary
{
public class ControllerManager : GameComponent, IService
{
    public GamePadState newState, oldState;
    public PlayerIndex player;

    public ControllerManager(Main game, PlayerIndex index)
        : base(game)
    {
        this.player = index;
    }
    public override void Update(GameTime gameTime)
    {
        oldState = newState;
        newState = GamePad.GetState(player);
        base.Update(gameTime);
    }
    public bool IsFirstButtonPress(Buttons button)
    {
        if (oldState.IsButtonUp(button) && newState.IsButtonDown(button))
            return true;

        return false;
    }

    public bool IsButtonDown(Buttons button)
    {
        return newState.IsButtonDown(button);
    }

    public bool IsButtonUp(Buttons button)
    {
        return newState.IsButtonUp(button);
    }

    #region Thumbsticks

    //Right

    //Magnitude of right stick in right direction
    public float RightStickRight()
    {
        if (newState.ThumbSticks.Right.X <= 0)
            return 0;
        else
            return newState.ThumbSticks.Right.X;
    }

    //Magnitude of right stick in left direction
    public float RightStickLeft()
    {
        if (newState.ThumbSticks.Right.X >= 0)
            return 0;
        else
            return System.Math.Abs(newState.ThumbSticks.Right.X);
    }

    //Magnitude of right stick in upward direction
    public float RightStickUp()
    {
        if (newState.ThumbSticks.Right.Y >= 0)
            return 0;
        else
            return System.Math.Abs(newState.ThumbSticks.Right.Y);
    }

    //Magnitude or right stick in downward direction
    public float RightStickDown()
    {
        if (newState.ThumbSticks.Right.Y <= 0)
            return 0;
        else
            return newState.ThumbSticks.Right.Y;
    }


    //Left

    //Magnitude of left stick in right direction
    public float LeftStickRight()
    {
        if (newState.ThumbSticks.Left.X <= 0)
            return 0;
        else
            return newState.ThumbSticks.Left.X;
    }

    //Magnitude of left stick in left direction
    public float LeftStickLeft()
    {
        if (newState.ThumbSticks.Left.X >= 0)
            return 0;
        else
            return System.Math.Abs(newState.ThumbSticks.Left.X);
    }

    //Magnitude of left stick in upward direction
    public float LeftStickUp()
    {
        if (newState.ThumbSticks.Left.Y >= 0)
            return 0;
        else
            return System.Math.Abs(newState.ThumbSticks.Left.Y);
    }

    //Magnitude or right stick in downward direction
    public float LeftStickDown()
    {
        if (newState.ThumbSticks.Left.Y <= 0)
            return 0;
        else
            return newState.ThumbSticks.Left.Y;
    }

    #endregion

    public bool RightStickCentered()
    {
        if (newState.ThumbSticks.Right.X.Equals(0) && newState.ThumbSticks.Right.Y.Equals(0))
        {
            return true;
        }
        return false;
    }

    public bool LeftStickCentered()
    {
        if (newState.ThumbSticks.Left.X.Equals(0) && newState.ThumbSticks.Left.Y.Equals(0))
        {
            return true;
        }
        return false;
    }

    public Vector2 RightStick()
    {
        return this.newState.ThumbSticks.Right;
    }

    public Vector2 LeftStick()
    {
        return this.newState.ThumbSticks.Left;
    }

    public bool LeftStickMoved()
    {
        if (!oldState.ThumbSticks.Left.X.Equals(newState.ThumbSticks.Left.X) || !oldState.ThumbSticks.Left.Y.Equals(newState.ThumbSticks.Left.Y))
        {
            return true;
        }

        return false;
    }

    public bool RightStickMoved()
    {
        if (!oldState.ThumbSticks.Right.X.Equals(newState.ThumbSticks.Right.X) || !oldState.ThumbSticks.Right.Y.Equals(newState.ThumbSticks.Right.Y))
        {
            return true;
        }

        return false;
    }
}
}

创建经理:

this.controller1Manager = new ControllerManager(this, PlayerIndex.One);
Components.Add(controller1Manager);
IServiceContainer.AddService(typeof(ControllerManager), controller1Manager);

this.controller2Manager = new ControllerManager(this, PlayerIndex.Two);
Components.Add(controller2Manager);
IServiceContainer.AddService(typeof(ControllerManager), controller2Manager);

更改值:

if (this.index.Equals(PlayerIndex.One))
    {
        this.localManager = Controller1Manager;
    }
else if (this.index.Equals(PlayerIndex.Two))
    {
        this.localManager = Controller2Manager;
        Controller2Manager.player = index;
    }

1 个答案:

答案 0 :(得分:0)

此问题已通过将ControllerManager传递到PlayerObject而不是PlayerIndex来解决。所有控制器管理器似乎立即更改其变量的原因是它们包含在上面编辑中显示的IServiceContainer中。 IServiceContainer只存储给定类型的一个实例(它是一个字典),因此只保存了最新的添加。