Да, код рабочий с жёлтой ошибкой тоже решил, присвоил
onready var sprite = $Sprite
onready var fon_01 = $Fon_01
action_suffix, это для определений назначенных клавиш влево/вправо через вкладку (настройки проекта -> список действий)... в коде ниже можешь посмотреть
class_name Player
extends Actor
signal collect_coin()
const FLOOR_DETECT_DISTANCE = 20.0
export(String) var action_suffix = ""
onready var platform_detector = $PlatformDetector
onready var animation_player = $AnimationPlayer
onready var shoot_timer = $ShootAnimation
onready var sprite = $Sprite
onready var fon_01 = $Fon_01
onready var gun = sprite.get_node(@"Gun")
func _ready():
# Static types are necessary here to avoid warnings.
var camera: Camera2D = $Camera
if action_suffix == "_p1":
camera.custom_viewport = $"../.."
yield(get_tree(), "idle_frame")
camera.make_current()
elif action_suffix == "_p2":
var viewport: Viewport = $"../../../../ViewportContainer2/Viewport2"
viewport.world_2d = ($"../.." as Viewport).world_2d
camera.custom_viewport = viewport
yield(get_tree(), "idle_frame")
camera.make_current()
func _physics_process(_delta):
var direction: Vector2 = get_direction()
fon_01.position.x -= direction.x
fon_01.position.y -= direction.y
# $Camera/fon_01.transform.origin.x -= direction.x
# $Camera/fon_01.transform.origin.y -= direction.y
_velocity = calculate_move_velocity(_velocity, direction, speed)
var snap_vector = Vector2.ZERO
if direction.y == 0.0:
snap_vector = Vector2.DOWN * FLOOR_DETECT_DISTANCE
var is_on_platform = platform_detector.is_colliding()
_velocity = move_and_slide_with_snap(
_velocity, snap_vector, FLOOR_NORMAL, not is_on_platform, 4, 0.9, false
)
# When the character’s direction changes, we want to to scale the Sprite accordingly to flip it.
# This will make Robi face left or right depending on the direction you move.
if direction.x != 0:
if direction.x > 0:
sprite.scale.x = 1
else:
sprite.scale.x = -1
# We use the sprite's scale to store Robi’s look direction which allows us to shoot
# bullets forward.
# There are many situations like these where you can reuse existing properties instead of
# creating new variables.
var is_shooting = false
#if Input.is_action_just_pressed("shoot" + action_suffix):
if Input.get_action_strength("shoot" + action_suffix):
is_shooting = gun.shoot(sprite.scale.x)
var animation = get_new_animation(is_shooting)
if animation != animation_player.current_animation and shoot_timer.is_stopped():
if is_shooting:
shoot_timer.start()
animation_player.play(animation)
func get_direction() -> Vector2:
return Vector2(
Input.get_action_strength("move_right" + action_suffix) - Input.get_action_strength("move_left" + action_suffix),
Input.get_action_strength("move_down" + action_suffix) - Input.get_action_strength("move_up" + action_suffix))
func calculate_move_velocity(linear_velocity, direction, speed):
var velocity = linear_velocity
velocity.x = speed.x * direction.x
velocity.y = speed.y * direction.y
return velocity
func get_new_animation(is_shooting = false):
var animation_new = ""
if is_on_floor():
if abs(_velocity.x) > 0.1:
animation_new = "run"
else:
animation_new = "idle"
else:
if _velocity.y > 0:
animation_new = "falling"
else:
animation_new = "jumping"
if is_shooting:
animation_new += "_weapon"
return animation_new