我正在尝试制作一款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表格中也有同样的问题,如果可能的话检查一下,以防有人在那里回答
https://godotforums.org/discussion/27007/my-get-viewport-get-mouse-position-isnt-working-right最新
如果你没有额外的Viewport
我的第一个直觉是得到get_global_mouse_position
并设置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
这种方法将使添加触摸支持变得更容易,因为它不依赖于任何提供鼠标位置的功能。
如果你有一个额外的Viewport
首先,确保将Viewport
放在ViewportContainer
中(否则它不会获得输入,请参阅Viewport内的节点未调用_input)。然后我们可以使用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
。它匹配的是Node
的name
,而不是类型。您可以尝试的另一种方法是使用get_viewport().get_parent()
.
是的,无论拉伸模式如何,这都应该工作。