鼠标单击并释放XNA [英] Mouse clicked and released XNA

查看:67
本文介绍了鼠标单击并释放XNA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作游戏的主菜单,而我在某个区域上点击(或单击鼠标左键)时遇到这些问题.我已经看到了WP7的许多游戏菜单,这些菜单在您释放点击后会起作用.好吧,我正在尝试在按下图像时显示图像,并在释放图像时发出声音.

I'm trying to make a main menu for a game and I'm having these problems when I tap(or left click) on an area . I've seen many game menus for WP7 that after you release the click it does something. Well I'm trying to display an image when it's pressed and make a sound when it's released.

这怎么可能?

这是我的代码:

//putoClick's = Mouse.GetState();
//clickarea = the same dimensions as putoClick; 
if (putoClick.LeftButton == ButtonState.Pressed)
{
    mouseClick = new Rectangle(putoClick.X, putoClick.Y, 30, 20);

    if (clickArea.Intersects(mouseClick))
    {
        spriteBatch.Draw(imgB, btnPos, Color.White);

        btnMenuClickSound.Play();

    }
    else
    {
        spriteBatch.Draw(imgBs, btnPosN, Color.White);
    }

    putoClick = Mouse.GetState();

    //....
}

无论何时按下按钮,声音都会持续播放.尝试使用新闻发布,但没有成功.对不起,我的变量名是一个快速测试.预先感谢

Instead the sound is constantly played when ever the the button is pressed. Tried working with the press and release, but didn't work. Excuse my variable names lol it's a quick test. Thanks in advance

推荐答案

您需要做的是创建一个保留两个鼠标状态的鼠标处理类.此帧和上一帧.并检查这两个状态之间按钮状态的变化.像

what you need to do is create a mouse handling class that retains two mouse states. this frame's and the previous frame's. and checks for changes in button states between these two states. something like

MouseState newState = Mouse.getState();    
if(oldState.LeftButton == ButtonState.Pressed && newState.LeftButton == ButtonState.Released){
    // click released
    // TODO : add your on released code here
}
if(newState.LeftButton == ButtonState.Pressed && oldState.LeftButton == ButtonState.Released)
{
    // pressed for the first time
    // TODO : add  your on clicked code here
}
if(newState.LeftButton == ButtonState.Pressed)
{ 
    // left mouse button is down
    // TODO : add your on left button down logic
}
// "remember" this frame's state for use in the next frame.
oldState = newState;

您每隔一帧(更新)调用此方法就完成了. (您可能希望通过使鼠标处理程序类仅更新内部布尔值并让您的游戏代码在需要时读取此布尔值来将逻辑输入完全分开,但这取决于您的游戏需求.

you call this method every frame (update) and you are done. (you may want to completely seperate input from logic by having the mouse handler class just update an internal boolean and having your game code read this boolean when needed, but that's up to your game needs.

这篇关于鼠标单击并释放XNA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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