瓷片碰撞检测——collision_tilemap

脚本说明

这是在reddit发现的一个脚本,作者为“CptLeafBlower
原帖地址:Super Easy Tile Collision - GMS2
跟内置函数“place_meeting”的用法十分类似,只是这个检测的不是对象而是瓷片地图
只需传入要检测的坐标点以及需要检测的瓷片地图ID即可

参数说明

参数序号 参数名 参数说明
argument0 x1 待检测坐标的x
argument1 y1 待检测坐标的y
argument2 x2 需要检测碰撞的瓷片地图ID

代码正文

///@description tiles_collision(x , y , tilemap)
///@arg x
///@arg y
///@arg tilemap

var xx, yy, tilemap, xp, yp, meeting;

xx = argument0;
yy = argument1;
tilemap = argument2;

//save our current position
xp = x;
yp = y;

//move to the position where we wanna check for a tile collision
x = xx;
y = yy;

//check for collision on all four corners of the collision mask
meeting =        tilemap_get_at_pixel(tilemap, bbox_right, bbox_top)
                ||
                tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom)
                ||
                tilemap_get_at_pixel(tilemap, bbox_left, bbox_top)
                ||
                tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom);

//Move back to the original position
x = xp;
y = yp;

//Return wether or not there was a collision
return(meeting);

使用范例

var tile_solid = layer_tilemap_get_id("tiles_ground")
if(!tiles_collision(x, y + 1, tile_solid))                            
{
    speed_down  += gravity_;
}

以上代码首先使用"layer_tilemap_get_id"的方法获取了"tiles_ground"图层中设置的瓷片地图的ID,然后将该ID保存到临时变量"tile_solid"中,然后检测在当前对象向下一个像素的位置有没有"tile_solid"对应的瓷片地图,如果没有则受重力加速度影响下落。

2018-01-26 21:48
Comments
Write a Comment