【问题标题】:Platformer Starter Kit camera scrolling issuePlatformer Starter Kit 相机滚动问题
【发布时间】:2013-06-14 20:17:08
【问题描述】:

我正在使用 XNA Platformer Kit 并且我正在尝试实现一个跟随玩家的相机。我关注David Amador's 2D Camera tutorial,相机按预期工作,它跟随玩家。但问题是,我的所有图块都不应该在“更新”方法中。

图块在屏幕上正确绘制,但是如果我尝试单击图块(我已经实现,如果您用鼠标单击图块,它会中断并消失)没有任何反应,但是如果我单击屏幕底部(在我实施相机之前绘制的瓷砖,它们应该消失。如果有人遇到这个问题,我希望得到一些帮助!

(就像我实现相机时鼠标位置不正确)

这是来自 Player 类更新方法(这是我进行更改的地方)

代码:

 MouseState mouseState = Mouse.GetState();
   int cellX = (int)(camera.Pos.X + mouseState.X) / Tile.Width;
   int cellY = (int)(camera.Pos.Y + mouseState.Y) / Tile.Height;
   if (cellX < Level.Width && cellX >= 0 && cellY < Level.Height && cellY >= 0)
   {
       if (Level.GetTileAt(cellX, cellY).Collision != TileCollision.Passable)
       {
           if (Level.tiles[cellX, cellY].isDead != true)
           {
               selectionHooverRectangle = Level.GetBounds(cellX, cellY);
               drawHooverRectangle = true;
               hooveredVaildTile = true;
           }
           else
           {
               drawHooverRectangle = false;
               hooveredVaildTile = false;
           }
       }
       else
       {
           drawHooverRectangle = false;
           hooveredVaildTile = false;
       }
   } 

   if (cellX < Level.Width && cellX >= 0 && cellY < Level.Height && cellY >= 0)
   {
       if (mouseState.LeftButton == ButtonState.Pressed)
       {
           Level.tiles[cellX, cellY].isDead = true;
       }
   }

【问题讨论】:

    标签: c# matrix xna camera 2d


    【解决方案1】:

    您需要在设置磁贴时将相机位置添加到鼠标位置。

    我假设你使用这样的东西来破坏/创建瓷砖,任何具有 X 和 Y 值的东西都可以工作,所以只需替换你的方法所需的东西。

    Tiles[MouseX / TileSize, MouseY / TileSize] = new Tile(...);
    

    现在要添加您的相机位置,如果您希望缩放和旋转也可以继续使用,我们将需要使用 matrix。然后我们只需添加翻译值。

    //Get the transformation matrix
    Matrix cameraTransform = cam.get_transformation(device /* Send the variable that has your graphic device here */));
    
    //Find the X and Y position by adding the matrix value to the mouse position, and scaling it down by your tile size
    float X = (-cameraTransform.Translation.X + MouseX) / TileSize;
    float Y = (-cameraTransform.Translation.Y + MouseY) / TileSize;
    
    //Do Stuff with the tile
    Tiles[(int)X,(int)Y] = new Tile(...);
    

    或者,如果你不关心旋转和缩放,你可以在没有矩阵的情况下添加相机位置

    float X = (cam.Position.X + MouseX) / TileSize;
    float Y = (cam.Position.Y + MouseY) / TileSize;
    
    //Do Stuff with the tile
    Tiles[(int)X,(int)Y] = new Tile(...);
    

    【讨论】:

    • 这似乎不起作用,如果我点击它们,它们不会消失,即使我点击屏幕底部(在实施相机之前的瓷砖位置)它们也不会消失。这是我的代码: MouseState mouseState = Mouse.GetState(); int cellX = (int)(-cameraTransform.Translation.X + mouseState.X) / Tile.Width; int cellY = (int)(-cameraTransform.Translation.Y + mouseState.Y) / Tile.Height; if (mouseState.LeftButton == ButtonState.Pressed) { Level.tiles[cellX, cellY].isDead = true; }
    • 我刚试了一下,它对我来说很好,除非我应用缩放或旋转,然后它开始变得有点奇怪。尝试调试它以确保您的 X 和 Y 值在合理范围内。
    • 不,它真的不适合我&我不知道为什么,在关卡更新方法中,相机的位置设置为玩家,所以相机跟随玩家,&在鼠标中单击平铺的东西我在上面尝试了您的代码,但它不起作用。鼠标坐标还是真的戴不准
    • 我编辑了我的问题,所以它现在有我杀死/删除tlies的代码或者你需要所有的代码吗?如果是这样,我会再次编辑它
    • 你可以添加一个断点来检查杀死块的代码是否被命中?另外,相机是否瞄准中心?意思是位置是相机的中心?如果是这样,您将需要减去一半的视口
    猜你喜欢
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2011-04-13
    • 2018-05-12
    相关资源
    最近更新 更多