Last active
April 15, 2018 17:26
-
-
Save WesleiRamos/2165d7b926a26d6413fee147bca6a5ca to your computer and use it in GitHub Desktop.
Godot KinematicBody2D example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends KinematicBody2D | |
## Gravidade vars | |
var VELOCIDADE = Vector2() | |
const FORCA_GRAVIDADE = Vector2(0, 1000) | |
## Andar vars | |
const VELOCIDADE_ANDAR = 120 | |
## Pular vars | |
var PULANDO = false | |
const PULO_FORCA = 400 | |
## Outros objetos | |
onready var PLAYER_SPRITE = get_node("player_sprite") | |
func _ready(): | |
pass | |
func _physics_process(delta): | |
# Adiciona gravidade | |
VELOCIDADE += FORCA_GRAVIDADE * delta | |
Andar() | |
Pular() | |
VELOCIDADE = move_and_slide(VELOCIDADE, Vector2(0, -1)) | |
func Andar(): | |
if Input.is_action_pressed("ui_left"): | |
PLAYER_SPRITE.flip_h = true | |
VELOCIDADE.x = -VELOCIDADE_ANDAR | |
elif Input.is_action_pressed("ui_right"): | |
PLAYER_SPRITE.flip_h = false | |
VELOCIDADE.x = VELOCIDADE_ANDAR | |
else: | |
VELOCIDADE.x = 0 | |
func Pular(): | |
# Caso PULANDO e esteja sobre o chão, então parou de pular | |
if PULANDO && is_on_floor(): | |
PULANDO = false | |
# Caso esteja apertando pra pular e não esteja pulando | |
# então pula | |
if Input.is_action_pressed("ui_up") && !PULANDO: | |
VELOCIDADE.y = -PULO_FORCA | |
PULANDO = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment