GameMaker Studio 2

创建于:2017-04-19

创建人: dougen

190 信息 1082 成员
游戏开发工具 GameMaker Studio 2 的讨论小组

[ 分享 ]【GML脚本】瓷片碰撞检测

顺子 2018-01-27

脚本说明

这是在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"对应的瓷片地图,如果没有则受重力加速度影响下落。

(转发自:原日志地址

近期喜欢的会员

 
highway★ 2018-02-25

帅,收了

 
Kendall 2018-07-19

感谢分享,收藏

 

加入 indienova

  • 建立个人/工作室档案
  • 建立开发中的游戏档案
  • 关注个人/工作室动态
  • 寻找合作伙伴共同开发
  • 寻求线上发行
  • 更多服务……
登录/注册