Wrap Around the Screen
Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 17 of 28.
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
EasyMake the robot come back in on the left.
- In
_process(), under the line that moves the robot, add anifforposition.xmore than640. - Inside it, set
position.xto0.
Press Play: the robot leaves on the right and comes back in on the left, at the same height. Then Check.
Try it yourself
extends Node2D
var speed = 200
func _process(delta):
position.x += speed * delta
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