From 70764a46a836c430e134738b76c6492c4fc08202 Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Sat, 22 Jan 2022 19:36:15 +0800 Subject: [PATCH] doc --- scene/demo05_scene_node/scene_node.tscn | 4 +-- script/demo05_scene_node/hero.gd | 40 ++++++++++++++----------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/scene/demo05_scene_node/scene_node.tscn b/scene/demo05_scene_node/scene_node.tscn index 3a887d7..7cc7874 100644 --- a/scene/demo05_scene_node/scene_node.tscn +++ b/scene/demo05_scene_node/scene_node.tscn @@ -4,9 +4,9 @@ [ext_resource path="res://icon.png" type="Texture" id=2] [node name="Node2D" type="Node2D"] -position = Vector2( 100, 0 ) +position = Vector2( 100, 100 ) [node name="Hero" type="Sprite" parent="."] -position = Vector2( 477.791, 270.893 ) +position = Vector2( 400, 200 ) texture = ExtResource( 2 ) script = ExtResource( 1 ) diff --git a/script/demo05_scene_node/hero.gd b/script/demo05_scene_node/hero.gd index 67859a1..76e5170 100644 --- a/script/demo05_scene_node/hero.gd +++ b/script/demo05_scene_node/hero.gd @@ -17,14 +17,20 @@ func _enter_tree(): pass +func _physics_process(delta): + setupGameWindow() + setupSprite() + positionTopCenter() + pass + # 屏幕坐标系 func windowPositionTest(): + # 设置游戏屏幕的位置 + # OS.window_position = Vector2(100, 100) + # OS.window_fullscreen = true + print(StringUtils.format("屏幕大小[{}]", [OS.window_size])) print(StringUtils.format("屏幕位置[{}]", [OS.window_position])) - - # 设置游戏屏幕的位置 - OS.window_position = Vector2(100, 100) - # OS.window_fullscreen = true pass # 坐标点测试用例 @@ -35,34 +41,34 @@ func positionTest(): print(to_local(global_position)) pass -func _physics_process(delta): - setupSprite() - setupGameWindow() - positionTopCenter() + +func setupGameWindow(): + gameWidth = OS.window_size.x + gameHeight = OS.window_size.y func setupSprite(): spriteWidth = texture.get_width() spriteHeight = texture.get_height() -func setupGameWindow(): - gameWidth = OS.window_size.x - gameHeight = OS.window_size.y func positionTopCenter() -> void: + #get_parent().position.x = 0 + #get_parent().position.y = 0 + #self.global_position self.position.x = gameWidth / 2 self.position.y = spriteHeight / 2 func positionCenter() -> void: - self.position.x = gameWidth / 2 - self.position.y = gameHeight / 2 + self.global_position.x = gameWidth / 2 + self.global_position.y = gameHeight / 2 func positionBottomCenter() -> void: - self.position.x = gameWidth / 2 - self.position.y = gameHeight - spriteHeight / 2 + self.global_position.x = gameWidth / 2 + self.global_position.y = gameHeight - spriteHeight / 2 func positionLeftCenter() -> void: - self.position.x = spriteWidth / 2 - self.position.y = gameHeight / 2 + self.global_position.x = spriteWidth / 2 + self.global_position.y = gameHeight / 2