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

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.

主站蜘蛛池模板: 中西区| 金堂县| 朝阳区| 陇西县| 毕节市| 历史| 册亨县| 广东省| 宁陕县| 澄城县| 宁陕县| 乐至县| 大连市| 南和县| 泾源县| 栖霞市| 金门县| 宁波市| 克什克腾旗| 彭山县| 揭东县| 洪湖市| 南宫市| 长葛市| 如东县| 易门县| 鲁山县| 鸡西市| 刚察县| 稻城县| 宜昌市| 武清区| 襄樊市| 巴林左旗| 丹凤县| 积石山| 咸丰县| 新疆| 安义县| 乌海市| 鄂伦春自治旗|