How to jump over platform step by step?

el_filipeel_filipe Posts: 3Member
edited May 15 in Programming

Hello
how to make jump from platform to platform like the game "Q*Bert's Qubes"?
At each keyboard entry, the player should move without interruption to a platform.
All platforms have the same width and spaced regularly (like a grid).
The player can also jump from 2 platforms at a time and can fall if there is no platform at his arrival. So each jump must have the same length. ex: 1 small jump move on 32px, and 1 long jump move on 64px.

A link to a video of "Q*Bert's Qubes": https://youtu.be/Pwr99PGyxV0

And a code that doesnt work because the move can be interrupted at any time even with the yield() and set_process() functions.
If you press several times right/left button, the player jumps higher and higher and never fall (or jump down) And small jump size are wrong.

extends KinematicBody2D

enum MOVES { NONE=0, LEFT,  RIGHT, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT }

const GRAVITY: int = 64
const SPEED: int = 150
const JUMP_HEIGHT: int = -64
const UP: Vector2 = Vector2(32, -1)

var _motion: Vector2 = Vector2()

func _ready():
    pass

func _physics_process(delta):
    var move = MOVES.NONE

    if Input.is_action_pressed("ui_left"):
        move = MOVES.LEFT
    elif Input.is_action_pressed("ui_right"):
        move = MOVES.RIGHT

    move_to(move)

func move_to(move: int):
    _motion.y += GRAVITY

    var anim: String = "idle"

    set_process(false)

    match move:
        MOVES.LEFT:
            anim = "jump_short"
            _motion.y = JUMP_HEIGHT
            _motion.x = -SPEED
        MOVES.RIGHT:
            anim = "jump_short"
            _motion.y = JUMP_HEIGHT
            _motion.x = SPEED
        _:
            _motion.x = 0

    $Anim.play(anim) #duration of "jump_short" = 0.7 sec
    _motion = move_and_slide(_motion, UP)

    yield($Anim, "animation_finished")

    set_process(true)

Thank you for your solutions!


Tags :

Comments

  • SchorschSchorsch Posts: 51Member

    I don't get your problem? That you can change the direction while the player is jumping? You can save the current state of the player and only trigger the jump if the key is hit, and the player is able to jump. So you have to move your "move" variable out of the process function. Then you have to check if move equals NONE and a jump-key is pressed. If the player lands, you have to set move back to NONE. Is that what you wanted?

  • el_filipeel_filipe Posts: 3Member

    I apologize, I misspoke.
    The character move only by bounce. And can bounce in 7 directions (see num MOVES{} in code).
    When we press a key, for example the right arrow key:
    The character makes a bounce to the platform on the right. As long as the player has not landed, no another movement are possible.
    It's only when it reaches a platform that we can move again the character.

Sign In or Register to comment.