首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XNA触觉定位

XNA触觉定位
EN

Stack Overflow用户
提问于 2014-03-23 22:47:01
回答 2查看 92关注 0票数 1

我在做泡泡包装游戏。一种你按下泡泡的方式,它们会在一小段时间后破裂。一切都很好,泡沫按计划破裂和破裂。不过,有一个问题。每当我点击并拖动气泡时,气泡仍会破裂。有什么方法可以让它们在我点击和拖动时不弹出?这是我的触摸位置代码。

代码语言:javascript
复制
foreach (TouchLocation tl in touches)
            {
                if (tl.State == TouchLocationState.Pressed)
                {
                    if (rectangle.Contains((int)tl.Position.X, (int)tl.Position.Y))
                    {
                        popLocationX = (int)tl.Position.X;
                        popLocationY = (int)tl.Position.Y;
                    }
                }
            }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-25 14:39:55

这是我所做的,我使用这个在我的主菜单上,但应该能够转移过来。

把这些加进去:

代码语言:javascript
复制
    MouseState mouse;
    public float mouseDelay = 0.01f;
    public float mouseTime = 0.0f;
    public bool mouseActive = true;
    public bool mouseUsed = false;
    public string mouseOn = "None";
    public Vector2 mouseLocation;
  • 鼠标帮助查找鼠标的位置。
  • 当鼠标处于按下的位置时,mouseDelay将停止重复操作。
  • mouseTime会说嗨,如果我比mouseDelay大,鼠标可以再用一次。
  • mouseActive会说它是否被按下。如果按下鼠标,mouseActive是假的。
  • mouseUsed将查看它是否曾经被使用过。如果鼠标只点击背景,那么它将是假的。如果是真的,那就意味着它被用来做某事。
  • mouseOn在我的主菜单中是用来表示你按下选项的?让mouseOn = "Options";和它去。
  • mouseLocation保存鼠标所在的位置。

所有这一切,把所有这些放在一起,将有助于你的目标,不让鼠标使用时,它已经被按下。

更新:

代码语言:javascript
复制
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        mouse = Mouse.GetState();

        if (this.IsMouseVisible == true)
        {
            MouseActive(gameTime);
        }
        base.Update(gameTime);
    }

我有如果鼠标是可见的,以阻止进程重复时,鼠标不存在(较少的处理能力)。

记住,这是为一个主菜单而做的,如果你想把它做成一个游戏,你需要把它弄得一团糟。

代码语言:javascript
复制
    void MouseActive(GameTime gameTime)
    {
        mouseTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

        mouse = Mouse.GetState();

        mouseLocation = new Vector2(mouse.X, mouse.Y);

        switch (gameState)
        {
            case GameStates.TitleScreen:
                break;
            case GameStates.Options:
                break;
            case GameStates.Credits:
                break;
        }

        if (mouseOn != "None")
        {
            mouseUsed = true;

            switch (gameState)
            {
                case GameStates.TitleScreen:
                    if (mouseOn == "Play")
                    {
                    }
                    if (mouseOn == "Quit")
                        this.Exit();
                    if (mouseOn == "Options")
                        gameState = GameStates.Options;
                    if (mouseOn == "Title")
                    {
                    }
                    break;
                case GameStates.Options:
                    break;
                case GameStates.Credits:
                    break;
            }

            mouseOn = "None";
        }

        if (mouse.LeftButton == ButtonState.Pressed)
        {
            mouseTime = 0.0f;
            mouseActive = false;
        }

        if (mouse.LeftButton == ButtonState.Released && mouseTime > mouseDelay)
        {
            mouseActive = true;
            mouseUsed = false;
        }
    }

上面的所有代码都将位于主类

下面是一个名为TitleScreen.cs的类,我使用了TitlesScreen上的图像和主类中的鼠标:

您首先需要初始化这个东西,所以在TitleScreen中添加这个方法:

代码语言:javascript
复制
    Game1 game1;

    public void Initialize(Game1 game1)
    {
        this.game1 = game1;
    }

您需要在主类(game1.cs)中添加一个初始化器

代码语言:javascript
复制
TitleScreen titlescreen;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        titlescreen = new TitleScreen();
    }

    protected override void Initialize()
    {
        titlescreen.Initialize(this);

        base.Initialize();
    }

然后,在最后一步中,将Update添加到TitleScreen:

代码语言:javascript
复制
    public void Update(GameTime gameTime)
    {
        MouseState mouse = Mouse.GetState();

        if (mouse.LeftButton == ButtonState.Pressed && game1.mouseUsed == false && game1.mouseActive == true)
        {
            if (play.Contains(mouse.X, mouse.Y))
            {
                game1.mouseOn = "Play";
                game1.mouseUsed = true;
            }
            else if (title.Contains(mouse.X, mouse.Y))
            {
                game1.mouseOn = "Title";
                game1.mouseUsed = true;
            }
            else if (options.Contains(mouse.X, mouse.Y))
            {
                game1.mouseOn = "Options";
                game1.mouseUsed = true;
            }
            else if (quit.Contains(mouse.X, mouse.Y))
            {
                game1.mouseOn = "Quit";
                game1.mouseUsed = true;
            }
        }
    }

您还需要将其添加到主类中的更新中

代码语言:javascript
复制
titlescreen.Update(gameTime);
票数 0
EN

Stack Overflow用户

发布于 2014-03-24 10:19:59

我还没有接触过触摸,但是所有输入方法的逻辑都应该是相似的。

我会创建一个变量来存储旧的TouchLocationState。这样,您可以检查用户是否正在单击和拖动。例如:

代码语言:javascript
复制
//TODO: instantiate this
TouchLocationState oldTLState; 

...

// This condition will only be true when the user clicks once, dragging will return false as the oldTLSTate is not released
if (tl.State == TouchLocationState.Pressed && oldTLState.State == TouchLocationState.Released)
{
    if (rectangle.Contains((int)tl.Position.X, (int)tl.Position.Y))
    {
        popLocationX = (int)tl.Position.X;
        popLocationY = (int)tl.Position.Y;
    }
}

//At the end of the Update() method, update oldTLState with the current state
oldTLState = tl;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22598157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档