pull/2/head
jaysunxiao 2022-01-22 19:36:15 +08:00
parent 5c8ba64d7e
commit 70764a46a8
2 changed files with 25 additions and 19 deletions

View File

@ -4,9 +4,9 @@
[ext_resource path="res://icon.png" type="Texture" id=2] [ext_resource path="res://icon.png" type="Texture" id=2]
[node name="Node2D" type="Node2D"] [node name="Node2D" type="Node2D"]
position = Vector2( 100, 0 ) position = Vector2( 100, 100 )
[node name="Hero" type="Sprite" parent="."] [node name="Hero" type="Sprite" parent="."]
position = Vector2( 477.791, 270.893 ) position = Vector2( 400, 200 )
texture = ExtResource( 2 ) texture = ExtResource( 2 )
script = ExtResource( 1 ) script = ExtResource( 1 )

View File

@ -17,14 +17,20 @@ func _enter_tree():
pass pass
func _physics_process(delta):
setupGameWindow()
setupSprite()
positionTopCenter()
pass
# 屏幕坐标系 # 屏幕坐标系
func windowPositionTest(): func windowPositionTest():
# 设置游戏屏幕的位置
# OS.window_position = Vector2(100, 100)
# OS.window_fullscreen = true
print(StringUtils.format("屏幕大小[{}]", [OS.window_size])) print(StringUtils.format("屏幕大小[{}]", [OS.window_size]))
print(StringUtils.format("屏幕位置[{}]", [OS.window_position])) print(StringUtils.format("屏幕位置[{}]", [OS.window_position]))
# 设置游戏屏幕的位置
OS.window_position = Vector2(100, 100)
# OS.window_fullscreen = true
pass pass
# 坐标点测试用例 # 坐标点测试用例
@ -35,34 +41,34 @@ func positionTest():
print(to_local(global_position)) print(to_local(global_position))
pass pass
func _physics_process(delta):
setupSprite()
setupGameWindow()
positionTopCenter()
func setupSprite():
spriteWidth = texture.get_width()
spriteHeight = texture.get_height()
func setupGameWindow(): func setupGameWindow():
gameWidth = OS.window_size.x gameWidth = OS.window_size.x
gameHeight = OS.window_size.y gameHeight = OS.window_size.y
func setupSprite():
spriteWidth = texture.get_width()
spriteHeight = texture.get_height()
func positionTopCenter() -> void: func positionTopCenter() -> void:
#get_parent().position.x = 0
#get_parent().position.y = 0
#self.global_position
self.position.x = gameWidth / 2 self.position.x = gameWidth / 2
self.position.y = spriteHeight / 2 self.position.y = spriteHeight / 2
func positionCenter() -> void: func positionCenter() -> void:
self.position.x = gameWidth / 2 self.global_position.x = gameWidth / 2
self.position.y = gameHeight / 2 self.global_position.y = gameHeight / 2
func positionBottomCenter() -> void: func positionBottomCenter() -> void:
self.position.x = gameWidth / 2 self.global_position.x = gameWidth / 2
self.position.y = gameHeight - spriteHeight / 2 self.global_position.y = gameHeight - spriteHeight / 2
func positionLeftCenter() -> void: func positionLeftCenter() -> void:
self.position.x = spriteWidth / 2 self.global_position.x = spriteWidth / 2
self.position.y = gameHeight / 2 self.global_position.y = gameHeight / 2