全球访问者状态机处理程序move_and_slide问题
我目前正在对我正在编码的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种称为
move_and_slide
的方法,但它属于kinematicbody2d
(或其3D对应方)。但是,您是在
node2d
中编写代码。正如错误消息告诉您的那样,对于node2d
,没有move_and_slide
。由于在
node2d
中没有move_and_slide
,因此您可以编写自己的方法称为move_and_slide
可以执行任何操作。但是,我相信您想使用一种表单Kinematicbody2d
。因此,将
node
从node2d
更改为kinematicbody2d
(并在顶部更新脚本,在其中说扩展了Node2d
说Kinematic Body2D
)。在Godot编辑器中,您可以找到一个选项,以更改称为“ Change Type”的ANode
的类型,它位于场景中的Node
的上下文菜单中。There is a method called
move_and_slide
but it belongs toKinematicBody2D
(or its 3D counterpart).However, you are writing code in a
Node2D
. As the error message tell you, there isn't amove_and_slide
forNode2D
.Since there isn't a
move_and_slide
inNode2D
, you could write your own method calledmove_and_slide
that does whatever you want. However, I believe you want to use the one formKinematicBody2D
.So change the
Node
fromNode2D
toKinematicBody2D
(and update the script, at the top, where it saysextends Node2D
to sayKinematicBody2D
). In the Godot editor you can find an option to change the type of aNode
called "Change Type", it is in the contextual menu of theNode
in Scene.您的类正在扩展Node2D,但
MOVE_AND_SLIDE
来自Kinematicbody2d
尝试:
并且还使用
move_and_slide
在_PHYSICS_UPDATE
函数中。另外,不要忘记将播放器节点更改为 Kinematicbody2d
,如果您的班级是 player 节点的孩子,请喜欢:
your class is extending Node2D but
move_and_slide
is fromKinematicBody2D
Try:
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: