godot - 我的 get_viewport().get_mouse_position() 不能正常工作 godot GDscript
问题描述
我正在尝试制作一个 2d 平台游戏,您可以在其中生成一个对象并尝试在它落下之前在空中跳跃。
问题是当我尝试生成它不会生成的图块时,它的光标是否会在我不知道如何摆脱它的位置添加一个相对值。
您会看到,当我尝试实例化一个场景时,它会考虑光标位置和视口值,但随后发生了一些事情,并且我发现对象生成得太远了。
这是我正在使用的脚本,它在 player1 场景中
extends KinematicBody2D
#
var score : int = 0
export var speed : int = 200
export var jumpforce : int = 600
export var gravity : int = 800
onready var AB1 = preload("res://player1AB.tscn")
var vel :Vector2 = Vector2()
onready var sprite : Sprite = get_node("sprite_idile")
onready var ui : Node = get_node("/root/mainscene1/CanvasLayer/ui")
onready var audioplayer : Node = get_node("/root/mainscene1/Camera2D/audio_player")
func _physics_process(delta):
vel.x = 0
# movement inputs
if Input.is_action_pressed("move_left"):
vel.x -= speed
if Input.is_action_pressed("move_right"):
vel.x += speed
# applying the velcoty
vel = move_and_slide(vel,Vector2.UP)
#apllying gravty
vel.y += gravity * delta
#jump input
if Input.is_action_just_pressed("jump") and is_on_floor():
vel.y -= jumpforce
# where the sprite facing
if vel.x < 0:
sprite.flip_h = true
if vel.x > 0:
sprite.flip_h = false
if Input.is_action_just_pressed("restart"):
death()
func death ():
get_tree().reload_current_scene()
func collect_coin (value):
score += value
ui.set_score_text(score)
audioplayer.play_coin_sfx()
func _input(event):
if event.is_action_pressed("click"):
var ABT1 = AB1.instance()
add_child(ABT1)
var XN = null
XN = get_viewport().get_mouse_position()
ABT1.position = XN
重要的东西
onready var AB1 = preload("res://player1AB.tscn")
func _input(event):
if event.is_action_pressed("click"):
var ABT1 = AB1.instance()
add_child(ABT1)
var XN = null
XN = get_viewport().get_mouse_position()
ABT1.position = XN
这是 Godot 形式中的相同问题,如果可能的话,请检查一下,以防有人在那里回答
解决方案
如果您没有额外的视口
我的第一直觉是 getget_global_mouse_position
和 set global_position
。这样您就不必处理任何相对定位:
ABT1.global_position = get_global_mouse_position()
或者,您可以检查事件是否为InputEventMouse
,使用 将其设为本地make_input_local
,然后从中获取InputEventMouse.position
:
func _input(event):
if event is InputEventMouse and event.is_action_pressed("click"):
var ABT1 = AB1.instance()
add_child(ABT1)
event = make_input_local(event)
ABT1.position = event.position
这种方法将使添加触摸支持变得更容易,因为它不依赖于任何为您提供鼠标位置的函数。请参阅我对在 godot 中保持屏幕触摸的回答。
如果您有额外的视口
首先,确保你把你的Viewport
内部 a ViewportContainer
(否则它不会得到输入,请参阅_input not called for a node inside a Viewport)。然后我们可以使用它ViewportContainer
来获取我们的坐标。
像这样的东西:
func _input(event):
if event is InputEventMouse and event.is_action_pressed("click"):
var ABT1 = AB1.instance()
add_child(ABT1)
var container = find_parent("ViewportContainer") as ViewportContainer
if container != null:
event = container.make_input_local(event)
event = make_input_local(event)
ABT1.position = event.position
请注意,我正在使用该功能find_parent
。它匹配name
的Node
,而不是类型。您可以尝试的另一种方法是使用get_viewport().get_parent()
.
是的,无论拉伸模式如何,这都应该有效。
推荐阅读
- elasticsearch - 添加 script.max_size_in_bytes: 10000000 不在 elastisearch 中考虑
- c# - 如何 Activator.CreateInstance 一个没有构造函数的类型?
- atom-editor - 是否可以根据文件扩展名对两个不同的包使用相同的键绑定?
- scala - 将 1 列拆分为多列时显示编译错误
- android - 用于图像的 URI 解析 Kotlin 似乎无法正常工作
- git - 从本地强制推送后,在 GitHub 站点上远程编辑的文件丢失
- java - 即使 SQL 服务器中有结果集,PreparedStatement.execute() 也会返回 false?
- html5-canvas - 如何在 FabricJS 中优化与大 svg 图像的显示和交互而没有延迟
- perl - 我在哪里可以在 Strawberry Perl 的配置中设置用户代理?
- apache-spark - hadoop 2.7.7 的 AWS-Java-SDK 版本问题