Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Canvas layers
Viewport and Canvas items
CanvasItem è la base per tutti i nodi 2D, siano essi nodi 2D regolari, come Node2D, o Control. Entrambi ereditano da CanvasItem. È possibile organizzare gli elementi canvas in alberi. Ogni elemento erediterà la trasformazione del suo genitore: quando il genitore si muove, si muovono anche i suoi figli.
I nodi CanvasItem e i nodi che ereditano da essi sono figli diretti o indiretti di una Viewport, che li visualizza.
La proprietà Viewport.canvas_transform della Viewport consente di applicare una trasformazione Transform2D personalizzata alla gerarchia CanvasItem che contiene. Nodi come Camera2D funzionano modificando tale trasformazione.
Per ottenere effetti come lo scorrimento, manipolare la proprietà di trasformazione del canvas è più efficiente che spostare l'elemento radice del canvas e l'intera scena con esso.
Solitamente, però, non vogliamo che tutto nel gioco o nell'applicazione sia soggetto alla trasformazione del canvas. Ad esempio:
Sfondi parallasse: sfondi che si muovono più lentamente rispetto al resto dello scenario.
Interfaccia utente: immaginate un'interfaccia utente (UI) o un display head-up (HUD) sovrapposto alla nostra visuale del mondo di gioco. Vogliamo che il contatore delle vite, il punteggio e altri elementi mantengano la loro posizione sullo schermo anche quando cambia la nostra visuale.
Transizioni: potremmo voler che gli effetti visivi utilizzati per le transizioni (dissolvenze, sfumature) rimangano in una posizione fissa sullo schermo.
Come risolvere questi problemi in un singolo albero di scene?
CanvasLayers
The answer is CanvasLayer, which is a node that adds a separate 2D rendering layer for all its children and grand-children. Viewport children will draw by default at layer "0", while a CanvasLayer will draw at any numeric layer. Layers with a greater number will be drawn above those with a smaller number. CanvasLayers also have their own transform and do not depend on the transform of other layers. This allows the UI to be fixed in screen-space while our view on the game world changes.
An example of this is creating a parallax background. This can be done with a CanvasLayer at layer "-1". The screen with the points, life counter and pause button can also be created at layer "1".
Here's a diagram of how it looks:
CanvasLayers are independent of tree order, and they only depend on their layer number, so they can be instantiated when needed.
Nota
CanvasLayers aren't necessary to control the drawing order of nodes. The standard way to ensuring that a node is correctly drawn 'in front' or 'behind' others is to manipulate the order of the nodes in the scene panel. Perhaps counterintuitively, the topmost nodes in the scene panel are drawn on behind lower ones in the viewport. 2D nodes also have the CanvasItem.z_index property for controlling their drawing order.