全球访问者状态机处理程序move_and_slide问题

发布于 2025-02-08 09:41:09 字数 1263 浏览 2 评论 0原文

我目前正在对我正在编码的statemachine遇到问题,发生的问题是,当我将move_and_slide添加到移动状态时,代码块的最后一个条件是遇到错误:

方法“ move_and_slide isn isn isn '当前课程中宣布

    extends Node2D

    class_name State

    var state_machine = null
    var player_velocity = Vector2()
    var player_speed = 200

    enum playerStates{IDLE, WALK, RUN, USE, SHOOT}
    enum ItemStates{ITEM_ON_GROUND, IS_EQUIPED}

   func _ready():
        if Global.player_isIdle == State.state_machine == playerStates.IDLE:
            pass
        if Global.player_isWalking == State.state_machine == playerStates.WALK:
            player_velocity = Global.player_velocity
    func handle_input(_event: InputEvent):
        if player_velocity != player_velocity.ZERO:
            if Global.player_isWalking == State.state_machine == playerStates.WALK:
                pass
            if Global.player_isRunning == State.state_machine == playerStates.RUN:
               if Input.is_action_pressed("sprint"):
                    player_speed = 300
            Global.player_velocity = Global.player_velocity.normalized() * player_speed
            Global.player_velocity = move_and_slide(Global.player_velocity)
    func _physics_process(_delta):
        pass

im currently having a problem with a statemachine that i am coding, the problem that is occurring is that when i add move_and_slide to the state of movement, the if condition at the last of the code block is getting the error:

the method " move_and_slide isn't declared in the current class

    extends Node2D

    class_name State

    var state_machine = null
    var player_velocity = Vector2()
    var player_speed = 200

    enum playerStates{IDLE, WALK, RUN, USE, SHOOT}
    enum ItemStates{ITEM_ON_GROUND, IS_EQUIPED}

   func _ready():
        if Global.player_isIdle == State.state_machine == playerStates.IDLE:
            pass
        if Global.player_isWalking == State.state_machine == playerStates.WALK:
            player_velocity = Global.player_velocity
    func handle_input(_event: InputEvent):
        if player_velocity != player_velocity.ZERO:
            if Global.player_isWalking == State.state_machine == playerStates.WALK:
                pass
            if Global.player_isRunning == State.state_machine == playerStates.RUN:
               if Input.is_action_pressed("sprint"):
                    player_speed = 300
            Global.player_velocity = Global.player_velocity.normalized() * player_speed
            Global.player_velocity = move_and_slide(Global.player_velocity)
    func _physics_process(_delta):
        pass

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断念 2025-02-15 09:41:09

有一种称为move_and_slide的方法,但它属于kinematicbody2d(或其3D对应方)。

但是,您是在node2d中编写代码。正如错误消息告诉您的那样,对于node2d,没有move_and_slide

由于在node2d中没有move_and_slide,因此您可以编写自己的方法称为move_and_slide可以执行任何操作。但是,我相信您想使用一种表单Kinematicbody2d

因此,将nodenode2d更改为kinematicbody2d(并在顶部更新脚本,在其中说扩展了Node2dKinematic Body2D)。在Godot编辑器中,您可以找到一个选项,以更改称为“ Change Type”的A Node的类型,它位于场景中的Node的上下文菜单中。

There is a method called move_and_slide but it belongs to KinematicBody2D (or its 3D counterpart).

However, you are writing code in a Node2D. As the error message tell you, there isn't a move_and_slide for Node2D.

Since there isn't a move_and_slide in Node2D, you could write your own method called move_and_slide that does whatever you want. However, I believe you want to use the one form KinematicBody2D.

So change the Node from Node2D to KinematicBody2D (and update the script, at the top, where it says extends Node2D to say KinematicBody2D). In the Godot editor you can find an option to change the type of a Node called "Change Type", it is in the contextual menu of the Node in Scene.

So尛奶瓶 2025-02-15 09:41:09
extends Node2D
class_name State

您的类正在扩展Node2D,但MOVE_AND_SLIDE来自Kinematicbody2d
尝试:

extends KinematicBody2D
class_name State

并且还使用move_and_slide_PHYSICS_UPDATE函数中。
另外,不要忘记将播放器节点更改为 Kinematicbody2d

,如果您的班级是 player 节点的孩子,请喜欢:

extends Node2D

class_name State

    var state_machine = null
    var player_velocity = Vector2()
    var player_speed = 200

    var parent = get_parent() <<<<<<<<<<<<<<<< 

    enum playerStates{IDLE, WALK, RUN, USE, SHOOT}
    enum ItemStates{ITEM_ON_GROUND, IS_EQUIPED}

   func _ready():
        if Global.player_isIdle == State.state_machine == playerStates.IDLE:
            pass
        if Global.player_isWalking == State.state_machine == playerStates.WALK:
            player_velocity = Global.player_velocity
    
func handle_input(_event: InputEvent):
        if player_velocity != player_velocity.ZERO:
            if Global.player_isWalking == State.state_machine == playerStates.WALK:
                pass
            if Global.player_isRunning == State.state_machine == playerStates.RUN:
               if Input.is_action_pressed("sprint"):
                    player_speed = 300
            Global.player_velocity = Global.player_velocity.normalized() * player_speed
            
    
func _physics_process(_delta):
    Global.player_velocity = parent.move_and_slide(Global.player_velocity) <<<<<<<<<<

extends Node2D
class_name State

your class is extending Node2D but move_and_slide is from KinematicBody2D
Try:

extends KinematicBody2D
class_name State

and also use move_and_slide in _physics_update function.
Also, don't forget to change Player Node to KinematicBody2D

and if your Class is a child of Player node then do like this:

extends Node2D

class_name State

    var state_machine = null
    var player_velocity = Vector2()
    var player_speed = 200

    var parent = get_parent() <<<<<<<<<<<<<<<< 

    enum playerStates{IDLE, WALK, RUN, USE, SHOOT}
    enum ItemStates{ITEM_ON_GROUND, IS_EQUIPED}

   func _ready():
        if Global.player_isIdle == State.state_machine == playerStates.IDLE:
            pass
        if Global.player_isWalking == State.state_machine == playerStates.WALK:
            player_velocity = Global.player_velocity
    
func handle_input(_event: InputEvent):
        if player_velocity != player_velocity.ZERO:
            if Global.player_isWalking == State.state_machine == playerStates.WALK:
                pass
            if Global.player_isRunning == State.state_machine == playerStates.RUN:
               if Input.is_action_pressed("sprint"):
                    player_speed = 300
            Global.player_velocity = Global.player_velocity.normalized() * player_speed
            
    
func _physics_process(_delta):
    Global.player_velocity = parent.move_and_slide(Global.player_velocity) <<<<<<<<<<

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文