0 votes

So. I want to make same movement as it is in Pokemon games. In some point character get running shoes and if u hold down KEY your character starts to run and u can change directions and still run (if it is pressed). I added something like that but my character is running while I press "K" but if i change direction inbetween movement it is back to normal speed even if "K" is still down"

So for example: I walk up. I hold down "K" and start running while holding down "K" i press right and it is back to normal speed even tho "K" is still down

func _input(event):
if event is InputEventKey:
    if event.is_pressed() == true and char(event.scancode) == "K":
        running = 0.1 #Run faster
    else:
        running = 0.2 #Run normal

-

extends Area2D

var tile_size = 32
var can_move = true
var facing = 'right'
var moves = {'right': Vector2(1,0),
             'left': Vector2(-1,0),
             'up': Vector2(0,-1),
             'down': Vector2(0,1)}
onready var raycasts = {
 'right': $RayCastRight,
 'left':  $RayCastLeft,
 'up':    $RayCastUp,
 'down':  $RayCastDown }

var running = 0.2

func _input(event):
    if event is InputEventKey:
        if event.is_pressed() == true and char(event.scancode) == "K":
            print("held")
            running = 0.1
        else:
            print("press")
            running = 0.2

func move(dir):
    facing = dir
    if raycasts[dir].is_colliding():
        return
    can_move = false
    $AnimationPlayer.play(facing, -1 , 2)
    $MoveTween.interpolate_property(self, "position", position, position + moves[facing] * tile_size, running, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
    $MoveTween.start()
    return true

func _on_MoveTween_tween_completed(object, key):
    can_move = true

also movement on player script:

extends "res://Area2D.gd"

func _process(delta):

    if Input.is_key_pressed(KEY_Q):
        get_tree().quit()

    if can_move:
        for dir in moves.keys():
            if Input.is_action_pressed(dir):
                move(dir)
in Engine by (12 points)
edited by

1 Answer

0 votes

this is what happened when you press right while holding k

the right key event is fired.

if event is InputEventKey: # it's true because it is a key event.
    # event.is_pressed() also true with `right` key.
    # `char(event.scancode) == "K"` is false because key is `right` so...
    if event.is_pressed() == true and char(event.scancode) == "K":
        running = 0.1 #Run faster
    else: # next processing should be here. running becomes normal.
        running = 0.2 #Run normal

if you want to change running variable only with k,
it should be like below.

if event.is_pressed() and char(event.scancode) == "K":
    running = 0.1
elif !event.is_pressed() and char(event.scancode) == "K":
    running = 0.2

or...

if char(event.scancode) == "K":
    if event.is_pressed():
         running = 0.1
    else:
         running = 0.2
by (9,539 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories