Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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# XNA按住并单击同一对象_C#_Xna_Mouse - Fatal编程技术网

C# XNA按住并单击同一对象

C# XNA按住并单击同一对象,c#,xna,mouse,C#,Xna,Mouse,如果游戏中的同一对象有两种不同的操作,使用默认输入(当前和最后一个MouseState)单击该对象并按住鼠标在其上单击,则即使按住鼠标,也会激活单击操作。例如,您可以使用按住键在屏幕上拖放对象,但当您单击它时,它应该消失。当你点击物体移动它的那一刻,它就会消失,这是一个不想要的结果 MouseState current, last; public void Update(GameTime gameTime) { current = Mouse.GetState(); bool c

如果游戏中的同一对象有两种不同的操作,使用默认输入(当前和最后一个MouseState)单击该对象并按住鼠标在其上单击,则即使按住鼠标,也会激活单击操作。例如,您可以使用按住键在屏幕上拖放对象,但当您单击它时,它应该消失。当你点击物体移动它的那一刻,它就会消失,这是一个不想要的结果

MouseState current, last;

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

   bool clicked = current.LeftButton == ButtonState.Pressed && last.LeftButton == ButtonState.Released;
   bool holding = current.LeftButton == ButtonState.Pressed;

   if(clicked) vanishObject();
   if(holding) moveObject(current.X, current.Y);

   last = current;
}

我想用一个“保持N秒以上”的标志来解决这个问题,并将消失放在ReleasedClick上,检查标志,但是改变事件时间似乎不是一个优雅的解决方案。有什么想法吗?

你应该改变你消失物体的逻辑。你真正想要的是

if (clicked && !holding) vanishObject();

这应该是获取所需内容的最简单方法。

您正在考虑单击错误的方法。实现此行为的最简单方法是,仅当在最后一帧按下mouseState并释放此帧时,以及如果未达到某个时间,才触发单击操作。代码:

private const float HOLD_TIMESPAN = .5f; //must hold down mouse for 1/2 sec to activate hold
MouseState current, last;
private float holdTimer;

public virtual void Update(GameTime gameTime)
{
    bool isHeld, isClicked; //start off with some flags, both false
    last = current; //store previous frame's mouse state
    current = Mouse.GetState(); //store this frame's mouse state
    if (current.LeftButton == ButtonState.Pressed)
    {
        holdTimer += (float)gameTime.ElapsedTime.TotalSeconds();
    }
    if (holdTimer > HOLD_TIMESPAN)
        isHeld = true;
    if (current.LeftButton == ButtonState.Released)
    {
        if (isHeld) //if we're releasing a held button
        {
            holdTimer = 0f; //reset the hold timer
            isHeld = false;
            OnHoldRelease(); //do anything you need to do when a held button is released
        }
        else if (last.LeftButton == ButtonState.Pressed) //if NOT held (i.e. we have not elapsed enough time for it to be a held button) and just released this frame
        {
            //this is a click
            isClicked = true;
        }
     }
     if (isClicked) VanishObject();
     else if (isHeld) MoveObject(current.X, current.Y);
 }
当然,这段代码还有一些优化的空间,但我认为它足够合理。

const float DragTimeLapsus=0.2f;
   const float DragTimeLapsus = 0.2f;

   if (current.LeftButton == ButtonState.Released) time = 0;
   else time+= ElapsedSeconds;

   bool clicked = current.LeftButton == ButtonState.Released
               && last.LeftButton == ButtonState.Pressed 
               && time<DragTimeLapsus;
   bool holding = current.LeftButton == ButtonState.Pressed 
               && last.LeftButton == ButtonState.Pressed 
               && time>DragTimeLapsus ;
如果(current.LeftButton==ButtonState.Released)时间=0; else time+=耗时秒; bool clicked=current.LeftButton==ButtonState.Released &&last.LeftButton==按钮状态。按下 &&时间差;
感谢所有回答我的人,但是没有人改变事实来实现发布事件上的点击,而不是新闻。我认为最好检查鼠标位置,如果realease与点击位置相同,它被点击,否则它被拖动