【问题标题】:Mouse click value won't update鼠标点击值不会更新
【发布时间】:2013-10-17 22:11:13
【问题描述】:

我正在制作一个简单的点击游戏,只是为了掌握鼠标输入类。

所以,我有一个按钮类(它只是一只猫的随机图片^^)和一个计数器类(计算我点击了多少次),游戏本身将是一个“Cookie Clicker”类型游戏的;D

但是当我从另一个类中使用一个变量时,它不会更新,即使它是公共的,这是我的类:

class catButton
{
    Texture2D buttonTexture;
    Vector2 buttonPosition;
    public Rectangle buttonRectangle;

    public MouseState currentMouseState;
    public MouseState previousMouseState;

    public void Initialize()
    {
        buttonPosition = new Vector2(150, 300);
    }

    public void LoadContent(ContentManager Content)
    {
        buttonTexture = Content.Load<Texture2D>("cat");
        buttonRectangle = new Rectangle((int)buttonPosition.X, (int)buttonPosition.Y, buttonTexture.Width, buttonTexture.Height);    
    }

    public void Update(GameTime gameTime)
    {
        previousMouseState = currentMouseState;
        currentMouseState = Mouse.GetState();  
    }

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
            spriteBatch.Draw(buttonTexture, buttonRectangle, Color.White);
    }      
}

还有我的柜台:

class catCounter
{
    SpriteFont catfont;
    Vector2 counterPosition;

    catButton button = new catButton();


    public MouseState currentMouseState;
    public MouseState previousMouseState;

    KeyboardState keyState = Keyboard.GetState();

    public float amountOfCats = 0;

    public catCounter()
    {

    }

    public void LoadContent(ContentManager Content)
    {
        counterPosition = new Vector2(205, 155);
        catfont = Content.Load<SpriteFont>("counter");
    }

    public void Update(GameTime gameTime)
    {
        MouseState currentMouseState = Mouse.GetState();

        if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released && button.buttonRectangle.Contains(new Point(currentMouseState.X, currentMouseState.Y)))
        {
            amountOfCats++;
        }

        previousMouseState = currentMouseState;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.DrawString(catfont, "Cats: " + amountOfCats, counterPosition, Color.White);
    }
}

我应该在Game1.cs 中单击鼠标吗?

抱歉,我写这篇文章的时间很短,如果你有什么知道的告诉我!

提前致谢!

【问题讨论】:

    标签: c# input xna


    【解决方案1】:

    当你从另一个类中使用它时,为什么变量不更新? 您一定是在错误地处理如何收集此变量。

    它是哪个变量?什么特定的调用应该更新它,但没有按预期进行?

    我假设您首先将catCounter 类初始化为 Game1.cs 文件中的一个对象,然后在 Game1.cs 中的相关方法中对​​该对象调用 Update()Draw() 来更新和绘制组件好吧,但只是提到它以防万一缺少。

    值得注意的是catButtonUpdate 方法只是一种浪费。只需删除其中的两行。它绝对没有任何作用,因为您不会在任何地方重复使用这些鼠标状态。同样在您的catCounter 更新中,您将currentMouseState 重新定义为该范围内的局部变量,从而使您的public MouseState currentMouseState 毫无价值且未被使用。

    不管怎样,关于 XNA 组件的一些注意事项。由于您的类目前遵循给定的 XNA 结构,因此您应该考虑为您的类继承 DrawableGameComponent

    class catCounter: DrawableGameComponent
    

    允许你做:

    Components.Add(new catCounter());
    

    这将使您不必手动调用Update()Draw() 方法,尽管您需要覆盖特定于它的方法(例如InitializeLoadContent、@ 987654336@, Draw)。

    很抱歉,当您不告诉我们问题出在哪里时,我无法理解您的问题所在。

    【讨论】:

      【解决方案2】:

      好的,我认为更好的方法是创建一个名为clicked 的变量并将其设置为False

      然后:

          public void Update(GameTime gameTime)
          {
              MouseState currentMouseState = Mouse.GetState();
      
              if (currentMouseState.LeftButton == ButtonState.Pressed)
              {
                  if (clicked == False && button.buttonRectangle.Contains(new Point(currentMouseState.X, currentMouseState.Y)))
                  {
                      amountOfCats++;
                      clicked = True;
                  }
              }
              else 
              {
                  clicked = False
              }
      
              // Not needed - previousMouseState = currentMouseState;
          }
      

      注意:不要忘记将 clicked 设为 bool 变量。

      这样,您可以防止用户按住鼠标,并且它不依赖于第二个鼠标状态变量。

      至于您的代码不起作用的原因是因为Mouse.GetState() 会自动更新。因此,previousMouseState 将始终是当前鼠标状态。

      我希望这会有所帮助。

      莫娜

      【讨论】:

        猜你喜欢
        • 2018-07-30
        • 2012-02-05
        • 2013-12-12
        • 2021-05-23
        • 1970-01-01
        • 2021-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多