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

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.

主站蜘蛛池模板: 集安市| SHOW| 巴林右旗| 泽库县| 秦皇岛市| 新竹县| 哈巴河县| 唐海县| 呼图壁县| 元谋县| 桃园县| 石台县| 克拉玛依市| 田阳县| 黑河市| 尼玛县| 孝昌县| 准格尔旗| 庐江县| 乌苏市| 汕头市| 敖汉旗| 泉州市| 宁都县| 隆德县| 友谊县| 桂阳县| 和平区| 丰顺县| 青铜峡市| 乌兰察布市| 上饶县| 湖北省| 吉隆县| 鸡东县| 淄博市| 涟源市| 江门市| 会昌县| 安图县| 额济纳旗|