godot-start/script/demo05_scene_node/hero.gd

69 lines
1.5 KiB
GDScript
Raw Normal View History

2021-10-03 01:02:45 +00:00
extends Sprite
var gameWidth: int
var gameHeight: int
var spriteWidth: int
var spriteHeight: int
2021-10-03 14:43:19 +00:00
func _enter_tree():
2021-10-04 08:49:12 +00:00
# textureTest()
# positionTest()
pass
# 坐标点测试用例
func positionTest():
print(position)
print(global_position)
print(to_global(position))
print(to_local(global_position))
pass
# 纹理测试用例
func textureTest():
2021-10-03 14:43:19 +00:00
var texture = get_texture()
var image = texture.get_data()
print(offset)
print("texture type: ", texture)
print("height: ", texture.get_height())
print("width: ", texture.get_width())
print("Image data property: ", image.data)
2021-10-04 08:49:12 +00:00
2021-10-03 14:43:19 +00:00
# 无法直接编辑图片
image.shrink_x2()
print("new image data property",image.data)
#这样做会破坏项目的资源,使资源变得混乱
#image.save_png("C:\\Users\\jaysunxiao\\Desktop\\newImage.png")
2021-10-03 01:02:45 +00:00
func _physics_process(delta):
setupSprite()
setupGameWindow()
positionTopCenter()
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:
self.position.x = gameWidth / 2
self.position.y = spriteHeight / 2
func positionCenter() -> void:
self.position.x = gameWidth / 2
self.position.y = gameHeight / 2
func positionBottomCenter() -> void:
self.position.x = gameWidth / 2
self.position.y = gameHeight - spriteHeight / 2
func positionLeftCenter() -> void:
self.position.x = spriteWidth / 2
self.position.y = gameHeight / 2