Wrap Around the Screen
Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 17 of 27.
A node moving right keeps going past the edge of the screen. Wrapping brings it back on the other side.
An if runs its lines only when its condition is true. In _process() it is checked on every frame:
func _process(delta):
position.y += 100 * delta
if position.y > 360:
position.y = 0The node falls. When it passes the bottom of the screen, y is bigger than 360, and it jumps back to the top. The line inside the if has two tabs: one for the function, one for the if.
This game is 640 pixels wide, so the right edge is x = 640.
Challenge
EasyThe robot moves right and leaves the screen. Make it wrap: when it passes x = 640, put it back at x = 0, on the left, at the same height.
Try it yourself
extends Node2D
var speed = 200
func _process(delta):
position.x += speed * delta
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Nodes and Scenes
1Your First Godot Game
What Is Godot?Ready Runs OncePrint in GodotThe Scene TreeSet a PositionMove a Node Once3Values You Can Tune
Variables on a NodeConstantsExport VariablesVector2 MathWrap Around the ScreenYour Own FunctionsPractice on your own: Godot playground