官术网_书友最值得收藏!

Script

Next, add a script to the Coin node. If you choose Empty in the Template setting, Godot will create an empty script without any comments or suggestions. The code for the coin's script is much shorter than the code for the player's:

extends Area2D

var screensize = Vector2()

func pickup():
queue_free()

The pickup() function is called by the player script and tells the coin what to do when it's been collected. queue_free() is Godot's node removal method. It safely removes the node from the tree and deletes it from memory along with all of its children. Later, you'll add a visual effect here, but for now the coin disappearing is good enough.

queue_free() doesn't delete the object immediately, but rather adds it to a queue to be deleted at the end of the current frame. This is safer than immediately deleting the node, because other code running in the game may still need the node to exist. By waiting until the end of the frame, Godot can be sure that all code that may access the node has completed and the node can be removed safely.

主站蜘蛛池模板: 安龙县| 牡丹江市| 房山区| 阿城市| 曲麻莱县| 城市| 叙永县| 佛山市| 台南县| 谷城县| 甘德县| 分宜县| 九龙城区| 同江市| 历史| 互助| 灵丘县| 苍溪县| 兰坪| 北票市| 抚松县| 博白县| 墨竹工卡县| 巨野县| 个旧市| 万山特区| 铁力市| 泰和县| 秦皇岛市| 新野县| 宣城市| 定襄县| 历史| 普定县| 五华县| 新津县| 余江县| 页游| 应城市| 卢龙县| 游戏|