Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.
Sprite turns right when I don't press any arrow on keyboardAlcedoHi, after the completion of the script when I left the arrow keys on the keyboard, the sprite turns on the right side but keeping the y position. I think this is due to the last line of code `_skin.flip_h = sign(direction.x) < 0.0`.
To prevent this behavior I added an If to check if there is no direction input. So my last lines of code are
`if direction.x != 0.0:`
` _skin.flip_h = sign(direction.x) < 0.0`
Do you guys think there is a better way to prevent this behavior?18Aug. 21, 2024
About the Dictionary techniqueWatchinofoyeAt the beginning of the lesson, you say "I will run you through different techniques to do that, starting with simple if and elif statements and ***moving on to a more advanced technique using a dictionary***."
Guess it have been missed because it does not appear in the lesson.
I presume it would be something where we have a dictionary using Vector directions as keys and textures as values?
Something like this :
```gdscript
var directions = {
Vector2.LEFT: RUNNER_RIGHT,
Vector2.RIGHT: RUNNER_RIGHT,
Vector2.UP: RUNNER_UP,
Vector2.DOWN: RUNNER_DOWN,
UP_LEFT: RUNNER_UP_RIGHT,
UP_RIGHT: RUNNER_UP_RIGHT,
DOWN_LEFT: RUNNER_DOWN_RIGHT,
DOWN_RIGHT: RUNNER_DOWN_RIGHT,
}
```
```gdscript
func _physics_process(delta: float) -> void:
# [...]
- match direction_discrete:
- Vector2.RIGHT, Vector2.LEFT:
- _skin.texture = RUNNER_RIGHT
- Vector2.UP:
- _skin.texture = RUNNER_UP
- Vector2.DOWN:
- _skin.texture = RUNNER_DOWN
- UP_RIGHT, UP_LEFT:
- _skin.texture = RUNNER_UP_RIGHT
- DOWN_RIGHT, DOWN_LEFT:
- _skin.texture = RUNNER_DOWN_RIGHT
+ if directions.has(direction_discrete):
+ _skin.texture = directions[direction_discrete]
if direction_discrete.length() > 0:
_skin.flip_h = sign(direction.x) < 0.0
```11Sep. 15, 2024
Sprite stattering after some framesPurpleSunriseHello! I am experiencing some stattering in the sprite after I leave it going in the same direction for a while, like after 1 secondo or so. It statters a couple of times and the stops. What could the reason be?
Thank you!80Dec. 04, 2024
Question about multiple cursor modeβ LP[https://i.imgur.com/Jb1knjh.png](https://i.imgur.com/Jb1knjh.png)
I want to copy the direction(LEFT, RIGHT, UP_LEFT, etc) all at once & paste it after RUNNER_.
So I need to enable multiple cursor on lines that are not adjacent to each other.
Would that possible?
Oh, and is there a shortcut to fold indented lines? I think that would solve my issue if we can only use multiple cursor with adjacent lines.20Dec. 03, 2024
Pseudo Private VariablesHodeoboBagginsI am at the pseudo private variable part of the lesson. I have this code. The displayed sprite never changes regardless of direction. Also if I just copy and paste code from the end of the lesson the displayed sprite never changes either. What am I missing?
```gdscript
extends CharacterBody2D
const RUNNER_DOWN = preload("res://assets/runner_down.png")
const RUNNER_DOWN_LEFT = preload("res://assets/runner_down_left.png")
const RUNNER_DOWN_RIGHT = preload("res://assets/runner_down_right.png")
const RUNNER_LEFT = preload("res://assets/runner_left.png")
const RUNNER_RIGHT = preload("res://assets/runner_right.png")
const RUNNER_UP = preload("res://assets/runner_up.png")
const RUNNER_UP_LEFT = preload("res://assets/runner_up_left.png")
const RUNNER_UP_RIGHT = preload("res://assets/runner_up_right.png")
@onready var _skin: Sprite2D = %Skin
var max_speed := 600.0'
var direction := Vector2(0, 0)
func _physics_process(_delta: float) -> void:
var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
velocity = direction * max_speed
if velocity.length() > 0.0:
rotation = velocity.angle()
if direction.x > 0.0 and direction.y > 0.0:
_skin.texture = RUNNER_DOWN_RIGHT
elif direction.x < 0.0 and direction.y > 0.0:
_skin.texture = RUNNER_DOWN_LEFT
move_and_slide()
```30Nov. 25, 2024
Question: Why does const DOWN_RIGHT work in Match?MrBright01When using match for character facing, why does DOWN_LEFT and the other constants work? Is matching for DOWN_LEFT a boolean check to see if DOWN_LEFT is true?30Nov. 16, 2024
"Try this on your own" if/elif chain question:MrBright01When I did this, I added the direction that was eliminated in the offered solution for the up/down/left/right straight movement and checked if it was zero. When I tested it, that eliminated the need to have the diagonals above the straights. Is there any reason I should not do this?
```gdscript
if direction.x > 0.0 and direction.y > 0.0:
_skin.texture = RUNNER_DOWN_RIGHT
elif direction.x < 0.0 and direction.y > 0.0:
_skin.texture = RUNNER_DOWN_LEFT
elif direction.x > 0.0 and direction.y < 0.0:
_skin.texture = RUNNER_UP_RIGHT
elif direction.x < 0.0 and direction.y < 0.0:
_skin.texture = RUNNER_UP_LEFT
elif direction.x > 0.0 and direction.y == 0.0:
_skin.texture = RUNNER_RIGHT
elif direction.x < 0.0 and direction.y == 0.0:
_skin.texture = RUNNER_LEFT
elif direction.x == 0.0 and direction.y > 0.0:
_skin.texture = RUNNER_DOWN
elif direction.x == 0.0 and direction.y < 0.0:
_skin.texture = RUNNER_UP
```20Nov. 16, 2024
Question, related to direction_discrete.lenght()oliver_gzzHey there, got a little lost, of what **direction_discrete.lenght()**, when it comes to simplify the code and flip textures horizontally.
Why shouldn't just be `if direction_discrete > 0:`
And why, is `if direction_discrete GREATER THAN 0` shouldn't it be, less than 0 because we're moving the character to the left?
thanks in advance!40Oct. 28, 2024
Question relating `sign()` functionAbdul Hannan AhmedHi!
I just wanted to ask that in the following code:
```gdscript
if direction_discrete.length() > 0:
_skin.flip_h = sign(direction.x) < 0.0
```
Why did we do `sign(direction.x)`. We already have a `direction_discrete: Vector2` which returns the `direction.sign()`. We could do:
```gdscript
if direction_discrete.length() > 0:
_skin.flip_h = direction_discrete.x < 0.0
```
Was there any specific reason to use `sign(direction.x)`?10Sep. 15, 2024
Lesson Q&A
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.