gdscript体会

前言:gdscript是Godot引擎提供的编程语言,代码结构上与Python类似

gdscript的match语句失效?

gdscript提供match实现其它编程语言的switch case效果,常见的结构如下

	var value = 0
	match value:
		-1:
			print("left")	
		0:
			print("none")	
		1:
			print("right")	

value与0匹配,输出none
但是在如下代码中获取控制输入的direction时,尽管debugger时变量direction的值在-101中,但是却没走匹配的逻辑,控制台没有输出"left""none""right"等信息

func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("move_left", "move_right")
	match direction:
		-1:
			print("left")	
		0:
			print("none")	
		1:
			print("right")	
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

默认情况下direction为0,声明一个为0的变量value与direction进行比较,在控制台输出为true
在这里插入图片描述
查看debugger窗口,directionvalue确实为0,但是match语句已经走到了29行,条件0没有匹配
在这里插入图片描述

难道是类型有什么问题吗?关注direction的来源Input.get_axis,方法声明中get_axis返回的是float类型
在这里插入图片描述

那把-1、0、1调整成-1.0、0.0、1.0能否正常匹配呢?
结果:成功匹配
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

松树戈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值