البطل: العودة إلى نقطة البداية
جزء من قسم الإدخال والحركة في رحلة 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)التحدي
متوسطخزّن موضع البداية في _ready(). عندما يسقط البطل إلى ما دون y = 400، أعده إلى هناك واضبط سرعته على 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