如何编写控制跳转并创建两次跳转动作的代码?

时间:2019-02-28 00:51:45

标签: godot gdscript

volumes:
      # this is needed to communicate with the postgres and redis services
      - ./superset_config.py:/home/superset/superset/superset_config.py
      # this is needed for development, remove with SUPERSET_ENV=production
      - ../../superset:/home/superset/superset
  

*下面的代码是我尝试计算跳转次数的地方,以防止它们超过两次跳转。我的目标是创造一个双重   跳跃,因此我使用了小于运算符来控制该数字*           如果jump_pressed:               如果jump_count <2:                   jump_count + = 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')

1 个答案:

答案 0 :(得分:0)

首先,我想如果您想使用KinematicBody2D,则需要使用_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(右键单击该节点,然后单击更改类型)。