学习日志|Godot零碎知识点若干

窗口透明化

在项目→项目设置→显示→窗口中勾选透明化和像素级透明。之后在脚本中添加以下代码:

#待补完

鼠标透过

@onready var polygon_2d_2: Polygon2D = $Polygon2D
……
DisplayServer.window_set_mouse_passthrough(polygon_2d_2.polygon)
之前写的笔记

研究了半天总算是明白为啥camara2D节点和Polygon2D节点在一起用显示不出了

首先是我用的图是一张像素图,原始大小很小,用polygon框选后显示在窗口中的实际大小也很小。要调整最终显示的大小不能用camara2D的缩放,而是必须调整图片和相应的Polygon大小,否则显示出来只有图片放大了而polygon范围不变,造成显示不全。

然后就是UP提到的原点对齐,在框选polygon范围的时候一定要保证它和Camara2D的原点对齐,完成后再移动polygon不会有影响。

最后还有一点,有时候透明化会失效,一般将渲染器改为兼容模式即可解决。

检测鼠标点击位置

#Rect2矩形区域
func _input(event: InputEvent):
	# 检测右键点击
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
			# 检查点击位置是否在特定区域内
			if Rect2(Vector2(100, 100), Vector2(200, 200)).has_point(event.position):
				# 显示右键菜单
				popup_menu.popup(Rect2i(event.position, Vector2i(100, 100)))
#Polygon2D多边形区域
func _input(event: InputEvent):
    if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
        # 获取鼠标位置(相对于当前节点的坐标系)
        var mouse_pos = get_local_mouse_position()
        # 检测点是否在多边形内
        if Geometry2D.is_point_in_polygon(mouse_pos, polygon_points):
            # 显示右键菜单
            popup_menu.popup(Rect2i(event.global_position, Vector2i(100, 100)))
上一篇
下一篇