forum.boolean.name

forum.boolean.name (http://forum.boolean.name/index.php)
-   Godot (http://forum.boolean.name/forumdisplay.php?f=181)
-   -   Выдаёт ошибку The property 'position' is not present (http://forum.boolean.name/showthread.php?t=20976)

Tiranas 01.10.2022 22:43

Выдаёт ошибку The property 'position' is not present
 
Godot 3.5
Выдаёт ошибки

Код:

W 0:00:01.778  The property 'position' is not present on the inferred type 'Node' (but may be present on a subtype).
  <Ошибка C++>  UNSAFE_PROPERTY_ACCESS
  <Источник>    Player.gd:54

W 0:00:01.778  The property 'position' is not present on the inferred type 'Node' (but may be present on a subtype).
  <Ошибка C++>  UNSAFE_PROPERTY_ACCESS
  <Источник>    Player.gd:55

Хотя такое свойство есть, и даже в быстром наборе
Вот кусок кода:
Код:

52: func _physics_process(_delta):
53:  var direction = get_direction()
54:  $Sprite.position.x -= direction.x
55:  $Sprite.position.y -= direction.y

func get_direction():
        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))


Arton 03.10.2022 07:27

Ответ: Выдаёт ошибку The property 'position' is not present
 
Код рабочий.
У меня только вопрос что в action_suffix подставляется, как это работает?

Загрузи минимальный пример с этой ошибкой, сцена + код + необходимые ресурсы.
Так как у тебя в проекте сделано.

Tiranas 03.10.2022 14:17

Ответ: Выдаёт ошибку The property 'position' is not present
 
Да, код рабочий с жёлтой ошибкой тоже решил, присвоил
Код:

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



Часовой пояс GMT +4, время: 14:18.

vBulletin® Version 3.6.5.
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Перевод: zCarot