我正在尝试制作一款约会模拟游戏,作为编程方面简单的第一个游戏。我不知道如何更改脚本中的角色精灵。
角色精灵.gd
extends Sprite
var char_tex = load("res://Sprites/Lu2.png")
func _ready():
set_texture(char_tex)
func _input(event):
if event is InputEventMouseButton:
char_tex = load("res://Sprites/Lu1.png")
update()
Run Code Online (Sandbox Code Playgroud)
只需将texture属性设置为所需的纹理即可。您还可以预加载纹理,然后只需切换它们而不用再次加载它们。
extends Sprite
var char_tex = load("res://Sprites/Lu2.png")
func _ready():
set_process_input(true)
texture = char_tex
func _input(event):
if event is InputEventMouseButton:
texture = load("res://Sprites/Lu1.png")
Run Code Online (Sandbox Code Playgroud)
您的示例中的问题是您只为char_tex变量分配了一个新图像,但这并没有改变精灵的纹理。仍将引用上一张图像,直到您使用或texture指定新图像。Gdscript 在这方面与 Python 比较相似,因此我建议您看一下 Ned Batchelder 的演讲Facts and Myths about Python Names and Values。texture =set_texture
| 归档时间: |
|
| 查看次数: |
18465 次 |
| 最近记录: |