【问题标题】:How should i go about making a Cursor Parallax effect in Monogame?我应该如何在 Monogame 中制作光标视差效果?
【发布时间】:2023-04-08 01:29:01
【问题描述】:

我在monogame subreddit中问过这个问题并得到了答案,但我很困惑。

我这样设置了一个数组:

Vector2D[] parallaxEffect = new Vector2D[2];

那么对于更新函数,代码如下

var mState = Mouse.GetState();
Vector2 translatorVector = mState.Position - _centerPosition;
//or _oldMousePosition

我对 _centerPosition 或 _oldMousePosition 有点困惑。我以为它是屏幕的中心,所以我像这样为它做了一个变量 Vector2 _centerPosition = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.Preferred.BackBufferHeight / 2); (无论我的游戏的游戏分辨率设置为屏幕中心。)

这只会导致错误..说

运算符“-”不能应用于“Point”和“Vector2D”类型的操作数

对于将产生这种视差效果的实际图像是:

_layerOffset[0] = translateVector * 1.0f

_layerOffset[1] = translateVector * 0.5f

在抽奖中

spriteBatch.Draw(textureLayer1 = new Vector2(400, 300)+ _layerOffset[1],Color.White();

spriteBatch.Draw(textureLayer0 = new Vector2(400, 300)+ _layerOffset[0],Color.White();

他说这是一个非常基本的原理,但我还是不太明白。这里有人可以帮忙吗?提前谢谢你!

【问题讨论】:

    标签: c# monogame


    【解决方案1】:

    你的代码在这里

    Vector2 translatorVector = mState.Position - _centerPosition;
    

    有错误,因为这里的 - 运算符可以处理相同的数据类型。而_centerPositionVector2 类型,mState.Position 是Point 类型。因此,您可以将mState.Position 转换为Vector2,如图所示

    Vector2 translatorVector = mState.Position.ToVector2() - _centerPosition;
    

    您似乎还有一些疑问,但我需要您提供更多代码/详细信息才能回答。不过,我觉得我可以尝试回答其中的一些问题。

    视差效果的原理是纹理的多个“层”以不同的速度移动(层越远,移动越慢)。我相信你输入的代码

    _layerOffset[0] = translateVector * 1.0f
    _layerOffset[1] = translateVector * 0.5f
    

    实现了这一点。你有两层以不同的速度移动。这里 translateVector 的大小取决于鼠标与屏幕中心的距离。 (因此我认为您的方法Vector2 _centerPosition = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.Preferred.BackBufferHeight / 2); 似乎是正确的)

    最后,您在最后键入的 Draw 方法似乎是部分的,因为必须有更多参数。我相信你只包含了 Vector2 对象,它指定了游戏需要在哪里绘制精灵/图像。这个 Vector2 的值是通过上面计算的每一层的位移并将其添加到一个 Constant 起点(在这种情况下,该点是 Vector2(400,300) )。

    还有 Color 参数,但现在不用担心。您可以使用其他颜色为您的图像/Texture2D 着色,或更改图像/Texture2D 的 alpha/透明度,但您的视差代码不需要它。因此,当您不想包含任何这些效果时,Color.White 只是您提供的默认参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多