Menu
CoddyTech

البطل: التوجّه في الاتجاه

جزء من قسم الإدخال والحركة في رحلة Godot على Coddy. الدرس 26 من 28.

يظل البطل مواجهًا لليمين عندما يركض إلى اليسار. اقلب عقدة Look بالطريقة التي تشير بها المفاتيح:

if direction > 0:
	$Look.scale.x = 1
if direction < 0:
	$Look.scale.x = -1

ضع هذه الأسطر بعد تعيين direction. عندما لا يكون أي مفتاح مضغوطًا، لن يُنفَّذ أيٌّ من شرطي if، لذا سيظل البطل متجهًا إلى آخر جهة واجهها.

challenge icon

التحدي

سهل

اجعل البطل يواجه الاتجاه الذي يركض نحوه.

  1. قبل move_and_slide()، اضبط $Look.scale.x على 1 عندما يكون direction > 0.
  2. اضبطه على -1 عندما يكون direction < 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)
	move_and_slide()

جميع دروس الإدخال والحركة

تدرّب بنفسك: Playground لـ Godot