البطل: العودة إلى نقطة البداية
جزء من قسم الإدخال والحركة في رحلة Godot على Coddy. الدرس 27 من 28.
إذا سقط البطل في الحفرة، فينبغي أن يعود إلى نقطة البداية.
خزّن نقطة البداية في متغير داخل _ready()، التي تعمل قبل أن يتحرك البطل:
var start_position = Vector2()
func _ready():
start_position = positionبعد move_and_slide()، تحقّق مما إذا كان البطل قد سقط أسفل الشاشة التي يبلغ ارتفاعها 360 بكسل. أعده إلى مكانه، وأعِد ضبط سرعته حتى لا يصل وهو لا يزال يسقط:
if position.y > 400:
position = start_position
velocity = Vector2(0, 0)التحدي
متوسطأعِد البطل إلى نقطة البداية عندما يسقط في الحفرة.
- أضف
var start_position = Vector2()، وفي دالة_ready()جديدة، عيّن له القيمةposition. - بعد
move_and_slide()، عندما يكونposition.y > 400، عيّنpositionإلىstart_positionوvelocityإلىVector2(0, 0).
اضغط «العب» واسقط في الحفرة: سيعود البطل إلى نقطة البداية. ثم اضغط «تحقق».
جرّب بنفسك
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()
جميع دروس الإدخال والحركة
5مشروع: بطل لعبة المنصات
البطل: الركضالبطل: القفزتدرّب بنفسك: Playground لـ Godot