通过窗口获取鼠标当前坐标
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))