The random() expression is used to find a random number.
- Random(X): Used to find a random number between 0 and X.
- Random(X,Y): Used to find a random number between X and Y.
Red_Square.Width and Red_Square.Height are expressions that return the width and height of the red square in pixels. Red_Square.X and Red_Square.Y are expressions that return the red square's X and Y position in the layout.
Since the sprite's origin is placed in the center of the sprite by default, we can find the edges by doing:
- Left Edge:
Red_Square.X-Red_Square.Width/2 (Lets call this A)
- Right Edge:
Red_Square.X+Red_Square.Width/2 (Lets call this B)
- Top Edge:
Red_Square.Y-Red_Square.Height/2 (Lets call this C)
- Bottom Edge:
Red_Square.Y+Red_Square.Height/2 (Lets call this D)
This is because since the origin is in the center, that means the edge of the sprite would be half of the sprites width/height away, yeah?
Now that we have the edges of the Red Square, we can just plug it into the random expression.
Using random(A, B) would get us a random point between the Left and the Right edges.
Using random(C, D) would get us a random point between the Top and the Bottom edges.
And that's pretty much how it works! Was I clear enough or should I explain more?