这个平台游戏滚动代码到底是做什么的?

发布于 2024-12-22 14:38:34 字数 1007 浏览 4 评论 0原文

我正在阅读现有平台游戏的源代码,我发现了这一点:

    /* assign the horizontal position of the TileMap on the screen to offsetX
     * this, center aligns the player. */
    int offsetX = width / 2 - Math.round(player.getX()) - TILE_SIZE;
    /* stop the map scrolling if the player reaches the two ends
     * of the map */
    offsetX = Math.min(offsetX, 0); // if offsetX < 0 , offsetX = 0
    offsetX = Math.min(offsetX, width-tileMap.getWidth()); //if offsetX > map width, offsetX = mapwidth

    int offsetY = height - toPixels(tileMap.getHeight()); // not really necessary, I think

    int firstTile = toTiles(-offsetX); // ???
    int lastTile = firstTile + toTiles(width) + 1; // why the +1

我评论了一些我认为我理解的部分,并在评论中提出了有关其他部分的问题。

最让我困扰的事情是:

1- 如何分配 offsetX (width/2 ... ?) 我知道它将 offsetX 分配到地图上与玩家中心对齐的某个位置,但是我不知道如何

2-在第3行,开发者为什么要写width -tileMap.getWidth()

注意:如果逐行解释代码太麻烦,请给我一个大概的想法,也许可以用图来表示?开发人员在这里试图做什么。谢谢。

I am reading the source code of an existing platformer game and I came accross this:

    /* assign the horizontal position of the TileMap on the screen to offsetX
     * this, center aligns the player. */
    int offsetX = width / 2 - Math.round(player.getX()) - TILE_SIZE;
    /* stop the map scrolling if the player reaches the two ends
     * of the map */
    offsetX = Math.min(offsetX, 0); // if offsetX < 0 , offsetX = 0
    offsetX = Math.min(offsetX, width-tileMap.getWidth()); //if offsetX > map width, offsetX = mapwidth

    int offsetY = height - toPixels(tileMap.getHeight()); // not really necessary, I think

    int firstTile = toTiles(-offsetX); // ???
    int lastTile = firstTile + toTiles(width) + 1; // why the +1

I have commented some parts I think I understand, and asked questions about others in the comments.

The things that bother me most are:

1- how offsetX is assigned (width/2 ... ?) I have understood that it assigns offsetX to some place on the map which center aligns the player, but I have no idea how.

2-In the 3rd line, why did the developer write width - tileMap.getWidth()?

Note: If it is too troublesome to explain the code line by line, please give me a rough idea, perhaps with a diagram? of what the developer is trying to do here. thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡间太子 2024-12-29 14:38:34

1-我认为这个偏移量是为了绘制图块而不是为了让玩家居中,这个函数在玩家移动时以负数增加偏移量,他移动得越远,越高 player.getX()< /code> 是,偏移量越高。

示例:

width=300
player.getX()=300
TILE_SIZE=10
tileMap[0]=(x,y)=(10,10)

offset=300/2-300-10
offset= -160

现在此偏移量用于绘制具有负偏移量的图块,换句话说,更左,在本例中 tileMap[0]=(10-160,10)
这意味着tileMap[0]超出了屏幕范围(图块滚动到左侧,播放器滚动到右侧)

2-我认为这应该是offsetX = Math.max(offsetX, width-tileMap.getWidth()) ;
在这种情况下,需要进行额外检查以仅滚动到地图的末尾。

例子:

    width=300
    player.getX()=900
    TILE_SIZE=10
    tileMap[last]=(1010,10)
    tileMap.getWidth()=1000

    offset=300/2-900-10
    offset= -760
    offsetX = Math.min(-760, 0);
    offsetX = -760
    offsetX = Math.max(-760, 300-1000);
    offsetX = -700

1-i think this offset is for drawing the tiles not for centering the player, this function increases the offset with a negative number when the player moves, the farther to the right he moves, the higher player.getX() is, the higher the offset is.

example:

width=300
player.getX()=300
TILE_SIZE=10
tileMap[0]=(x,y)=(10,10)

offset=300/2-300-10
offset= -160

now this offset is used to draw the tiles with a negative offset, in other words, further left, in this case tileMap[0]=(10-160,10)
which means tileMap[0] is out of the screen range(tile scrolled to the left, player to the right)

2-i think this should be offsetX = Math.max(offsetX, width-tileMap.getWidth());
in that case its an extra check to only scroll to the end of the map.

example:

    width=300
    player.getX()=900
    TILE_SIZE=10
    tileMap[last]=(1010,10)
    tileMap.getWidth()=1000

    offset=300/2-900-10
    offset= -760
    offsetX = Math.min(-760, 0);
    offsetX = -760
    offsetX = Math.max(-760, 300-1000);
    offsetX = -700
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文