【发布时间】:2018-01-10 03:54:18
【问题描述】:
Game Maker Studio 2 有一个可下载的地牢自上而下游戏演示。其中有一个碰撞检查脚本。向左走的部分如下:
x += -4;
var tx = (x-16)>>5; /* check right edge (my note: this gets the tile x-index
for the wall on the left, as each tile is 32 pixels wide. I think they
copied the part for walking right and didn't fix the comment)*/
var ty1 = ((y+16)>>5);
var ty2 = ((y-16)>>5);
// collision data never has flips etc...
var tile1 = tilemap_get(WallMap, tx,ty1 )& tile_index_mask;
var tile2 = tilemap_get(WallMap, tx,ty2 )& tile_index_mask;
if(( tile1!=0 ) || (tile2!=0))
{ x = (x&~31)+16; }
(我用它们的值替换了一些变量和宏。+16 部分用于精灵调整。玩家的精灵原点 (x,y) 设置为距离其边缘 16 像素)。
最后一行让我很困扰。
31 位是 000000000000000000000000000011111。
~31 位然后是 11111111111111111111111111100000。
所以 x&~31 实际上应该只是将 x 位的最后 5 位设置为 0。
但是 x=0,y=0 是房间的左上角。 x 越小,越靠近左侧。
所以我从中收集到的是,当 x 在向左行走时与左侧的墙壁发生碰撞时,此函数将其原点设置为更靠近左侧,进入墙壁。这没有意义。
据我了解,最后一行应该是 { x = (x&~31)+32+16; },要将玩家原点捕捉到墙砖的左侧,将其移动到墙的右侧 32 像素,然后再移动 16 像素以调整精灵宽度。
但运行游戏,它按预期工作。试图向左走入一堵墙可以让你保持原位。我哪里看错了?
【问题讨论】:
标签: bit-manipulation bitwise-operators bitwise-and