这个平台游戏滚动代码到底是做什么的?
我正在阅读现有平台游戏的源代码,我发现了这一点:
/* 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1-我认为这个偏移量是为了绘制图块而不是为了让玩家居中,这个函数在玩家移动时以负数增加偏移量,他移动得越远,越高
player.getX()< /code> 是,偏移量越高。
示例:
现在此偏移量用于绘制具有负偏移量的图块,换句话说,更左,在本例中
tileMap[0]=(10-160,10)
这意味着tileMap[0]超出了屏幕范围(图块滚动到左侧,播放器滚动到右侧)
2-我认为这应该是offsetX = Math.max(offsetX, width-tileMap.getWidth()) ;
在这种情况下,需要进行额外检查以仅滚动到地图的末尾。
例子:
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:
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: