Held: Zurück zum Start
Teil des Abschnitts Eingabe und Bewegung der Godot-Journey von Coddy. Lektion 27 von 28.
Wenn der Held in die Grube fällt, sollte er zum Start zurückkehren.
Speichere den Start in einer Variable in _ready(), das ausgeführt wird, bevor sich der Held bewegt:
var start_position = Vector2()
func _ready():
start_position = positionPrüfe nach move_and_slide(), ob der Held unter den 360 Pixel hohen Bildschirm gefallen ist. Setze ihn zurück und setze seine Geschwindigkeit zurück, damit er nicht weiterhin fallend ankommt:
if position.y > 400:
position = start_position
velocity = Vector2(0, 0)Aufgabe
MittelSetze den Helden an den Anfang zurück, wenn er in die Grube fällt.
- Füge
var start_position = Vector2()hinzu und setze es in einem neuen_ready()aufposition. - Setze nach
move_and_slide(), wennposition.y > 400gilt,positionaufstart_positionundvelocityaufVector2(0, 0).
Drücke „Spielen“ und falle in die Grube: Der Held kommt am Anfang zurück. Drücke dann „Prüfen“.
Probier es selbst
extends CharacterBody2D
var speed = 220
var acceleration = 1200
var friction = 2400
var gravity = 1000
var jump_velocity = -500
func _physics_process(delta):
velocity.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
var direction = Input.get_axis("move_left", "move_right")
if direction == 0:
velocity.x = move_toward(velocity.x, 0, friction * delta)
else:
velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
if direction > 0:
$Look.scale.x = 1
if direction < 0:
$Look.scale.x = -1
move_and_slide()
Alle Lektionen in Eingabe und Bewegung
2CharacterBody2D
CharacterBody2DMit Geschwindigkeit laufenWände halten dich aufÜberall laufenAusgewogene Diagonalen5Projekt: Plattformheld
Held: LaufenHeld: SpringenÜbe selbstständig: Godot-Playground