godot 4 代码片段

本文介绍了Godot 4中如何获取鼠标坐标、处理鼠标点击事件,以及动态创建和播放动画,涵盖了游戏开发中的关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过窗口获取鼠标当前坐标

var mouse_position:Vector2  = get_viewport().get_mouse_position()
print(mouse_position.x)
print(mouse_position.y)

通过内置虚函数获取鼠标点击事件

func _input(event) :
   if event is InputEventMouseButton :
   	if event.button_index == MOUSE_BUTTON_LEFT :
   		if event.pressed :
   			print("鼠标左键点击事件")
   		else:
   			print("鼠标左键释放事件")
   	if event.button_index == MOUSE_BUTTON_RIGHT :
   		if event.pressed :
   			print("鼠标右键点击事件")
   		else:
   			print("鼠标右键释放事件")
   	if event.button_index == MOUSE_BUTTON_MIDDLE :
   		print("鼠标中键点击事件")
   	if event.button_index == MOUSE_BUTTON_WHEEL_UP :
   		print("鼠标滚轮向上滚动事件")
   	if event.button_index == MOUSE_BUTTON_WHEEL_DOWN :
   		print("鼠标滚轮向下滚动事件")

获取某个动作动画集(代码动态创建)

func  get_animations(texture_name, texture_size, sprite_size):
   var animations = {}
   var full_texture = load("res://素材/人物/" + character_name + "/" + texture_name + ".png")
   var num_columns = int(texture_size.x / sprite_size.x)
   var num_rows = int(texture_size.y / sprite_size.y)
   
   for x in range(num_rows):
   	var sprite_frame = SpriteFrames.new()
   	sprite_frame.add_animation(texture_name + "_" + String.num(x))
   	sprite_frame.set_animation_speed(texture_name + "_" + String.num(x),10)
   	sprite_frame.set_animation_loop(texture_name + "_" + String.num(x),true)
   	for y in range(num_columns):
   		var frame = AtlasTexture.new()
   		frame.atlas = full_texture
   		frame.region = Rect2(Vector2(y , x) * sprite_size,sprite_size)
   		sprite_frame.add_frame(texture_name + "_" + String.num(x),frame)
   	animations[texture_name + "_" + String.num(x)] = sprite_frame
   	
   return animations

播放某个方向的站立动画

# 获取全部站立动画
func  get_stand_animations():
   return get_animations(stand,stand_texture_size,stand_sprite_size)

# 播放某个方向的站立动画
func  play_one_direction_stand_annimation():
   var stands = get_stand_animations()
   animate.sprite_frames.clear_all()
   animate.sprite_frames = stands.get(stand + "_" + String.num(annimation_index))
   animate.play(stand + "_" + String.num(annimation_index))
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值