godot - 如何编写控制跳跃并创建双跳动作的代码?
问题描述
# changed switch statement setup by creating a new variable and using "." operator
# removed extra delta in move_and_slide function
# left off attempting to add gravity to the game
# 2/26/2019
# removed FSM and will replace it with tutorial video code, for sake of completion
extends Node2D
const FLOOR = Vector2(0,-1)
const GRAVITY = 5
const DESCEND = 0.6
var speed = 100
var jump_height = -250
var motion = Vector2()
var jump_count = 0
var currentState = PlayerStates.STATE_RUNNING
var grounded = false
enum PlayerStates {STATE_RUNNING, STATE_JUMPING, STATE_DOUBLE_JUMPING, STATE_GLIDING}
func _ready():
var currentState = PlayerStates.STATE_RUNNING
pass
func jump():
motion.y = jump_height
func glide():
if motion.y < 500:
motion.y += DESCEND
func _process(delta):
var jump_pressed = Input.is_action_pressed('jump')
var glide_pressed = Input.is_action_pressed('glide')
* 下面的代码是我尝试计算跳跃的地方,以防止它们超过两次跳跃。我的目标是创建一个双跳,因此我使用小于运算符来控制该数字* if jump_pressed: if jump_count < 2: jump_count += 1 jump() grounded = false <-- 我不得不再次复制粘贴此代码,下面,所以我的问题没有错误。
if jump_pressed:
if jump_count < 2:
jump_count += 1
jump()
grounded = false
if grounded == false:
if glide_pressed:
glide()
motion.x = speed
motion.y += GRAVITY
motion = move_and_slide(motion, FLOOR)
if is_on_floor():
grounded = true
jump_count = 0
else:
grounded = false
解决方案
首先,我认为如果你想使用 aKinematicBody2D
并执行你的逻辑,除此之外,你的代码几乎可以工作:_physics_process
move_and_slide
extends KinematicBody2D
const FLOOR = Vector2(0,-1)
const GRAVITY = 3000
const DESCEND = 0.6
var speed = 100
var jump_height = 250
var motion = Vector2()
var jump_count = 0
func jump():
motion.y = -jump_height
func glide():
if motion.y < 500:
motion.y += DESCEND
func _physics_process(delta):
var jump_pressed = Input.is_action_pressed('jump')
var glide_pressed = Input.is_action_pressed('glide')
motion.y += delta * GRAVITY
var target_speed = Vector2(speed, motion.y)
if is_on_floor():
jump_count = 0
if glide_pressed:
glide()
if jump_pressed and jump_count < 2:
jump_count += 1
jump()
motion = lerp(motion, target_speed, 0.1)
motion = move_and_slide(motion, FLOOR)
您还需要在 Godot 编辑器中将节点类型更改为 KinematicBody2D(右键单击节点,然后更改类型)。
推荐阅读
- linux - 如何在 Linux Ubuntu 中连接以太网?
- shell - 删除所有空目录但保留根目录
- ios - MacOS CI 存档
- c# - 在 C# 中使用虚拟集合使代码更高效
- windows - 当 Windows MSI 包在磁盘上的位置与旧版本不同时,安装失败并出现错误 1603
- mysql - XAMPP:尽管端口未被阻止,但 MySql 未启动
- c# - 删除目录,但保留其所有内容,包括(子目录和文件)
- python - Carnets 应用程序的挑战无法为 statsmodel 和 SciPy 安装库(Ipad Pro 上的 jupyter)
- php - Laravel excel:下载速度慢,即使有队列
- sql - 如何从列中选择特定字符串?