首页 > 解决方案 > 如何让精灵在单人游戏中从一个位置到鼠标点击进行可见的移动?

问题描述

我正在制作一个火球,它正在移动到鼠标点击的位置。火球精灵已经移动到该位置,但我看不到火球飞行。我想看看,火球“飞”到那个位置?

这是我的monogame代码,用c#编写

protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here
            var CurrentMouseState = Mouse.GetState();
            PreviousMouseState = CurrentMouseState;
            if (CurrentMouseState.LeftButton == ButtonState.Pressed)
            {
                ballPosition = new Vector2(CurrentMouseState.X, CurrentMouseState.Y);

            }

            base.Update(gameTime);
        }

有人可以帮助我吗?我没有在其他问题上找到帮助。

标签: c#mouseeventspritemonogame

解决方案


到目前为止,球正在立即移动到鼠标的位置。但现在它需要自行移动到鼠标位置。

假设“飞向鼠标”,您的意思是沿直线移动到鼠标位置。您可以尝试使用Vector2.Normalize来决定它需要向鼠标移动的方向和角度。

试试这里找到的答案:https ://gamedev.stackexchange.com/a/7757/96707


推荐阅读