1
# 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
4

1 回答 1

0

首先,我认为如果你想使用 aKinematicBody2D并执行你的逻辑,除此之外,你的代码几乎可以工作:_physics_processmove_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(右键单击节点,然后更改类型)。

于 2019-03-07T15:16:57.723 回答