【问题标题】:How do I write a code that controls jumps, and creates a double jump action'?如何编写控制跳跃并创建双跳动作的代码?
【发布时间】:2019-03-07 23:16:57
【问题描述】:
# 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')

* 下面的代码是我尝试计算跳跃次数的地方,以防止它们超过两次跳跃。我的目标是创造一个双 跳转,所以我使用小于运算符来控制该数字* 如果 jump_pressed: 如果 jump_count

    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

【问题讨论】:

    标签: godot gdscript


    【解决方案1】:

    首先,如果你想使用move_and_slide,我认为你需要使用KinematicBody2D并在_physics_process中执行你的逻辑,除此之外,你的代码几乎可以工作:

    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(右键单击节点,然后更改类型)。

    【讨论】:

      猜你喜欢
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多