My character move only a few pixels in "Coding the player" in godot 4.1 tutorial. And the animations. GDScript

93 views Asked by At

I am trying to make my character to move. It actually moves in the right direction and the animation only activates the first frame. (There is two animations with two frames each: up and walk to the right, and with code flips when you move the other way as you see in my code). But when it moves, it does onle a few pixels.

I tried to change the speed but the characthers instead of move smoothly moves in jumps.

Here is my code in gdscript:

extends Area2D

signal hit

@export var speed = 4000
var screen_size # Size of the game window.

func _ready():
    screen_size = get_viewport_rect().size
    #hide()

func _process(delta):
    var velocity = Vector2.ZERO #The player's movement vector.
    if Input.is_action_just_pressed("move_down"):
        velocity.y += 1
    if Input.is_action_just_pressed("move_up"):
        velocity.y -= 1
    if Input.is_action_just_pressed("move_right"):
        velocity.x += 1
    if Input.is_action_just_pressed("move_left"):
        velocity.x -= 1
        
    if velocity.length() > 0:
        velocity = velocity.normalized() * speed
        $AnimatedSprite2D.play()
    else:
        $AnimatedSprite2D.stop()
    position += velocity * delta
    position = position.clamp(Vector2.ZERO, screen_size)
    
    
    if velocity.x != 0:
        $AnimatedSprite2D.animation = "walk"
        $AnimatedSprite2D.flip_v = false
        # See the note below about boolean assignment.
        $AnimatedSprite2D.flip_h = velocity.x < 0
    elif velocity.y != 0:
        $AnimatedSprite2D.animation = "up"
        $AnimatedSprite2D.flip_v = velocity.y > 0


func _on_body_entered(body):
    hide() #Player dissappears after being hit.
    hit.emit()
    # Must be deferred as we can't change physics properties on a physics callback.
    $CollisionShape2D.set_deferred("disabled", true)

func start(pos):
    position = pos
    show()
    $CollisionShape2D.disabled = false

With speed of 400. There is not much difference between With speed of 4000. The second image is the place where the character moves only when I press the arrow of the keyboard.

The characther without moving

The characther with only one touch of the arrow. There wasn't a smooth move, only the jump The characther with only one touch of the arrow. There wasn't a smooth move, only the jump.

1

There are 1 answers

0
Theraot On

The method is_action_just_pressed returns true only the first frame the action is pressed. Use is_action_pressed instead.