CodeForeverAndEver
4/21/2020 - 4:04 PM

How does the scrolling work in the Arcade Library?

How does the scrolling work in the Arcade Library?

Intro

If you've used the arcade library, then you've definitely had to have had a scrolling background before. Except, the calculations can be a little confusing, so I am going to explain them!

How it works

Let's look at this code:

right_boundary = self.view_left + RIGHT_VIEWPORT_MARGIN
        if self.player_sprite.right > right_boundary:
            self.view_left += self.player_sprite.right - right_boundary
            changed = True

This is an example of the code that you will see here: Scrolling in Arcade Docs

So, what is happening? It's quite simple actually:

Basically, the right boundary is going to equal the left side of the screen plus the padding (margin). The player will always be right to the left that margin because if he crosses it, it moves forward again, it's like he's chasing it. When he crosses it, the left view is going to equal the right side of the character, minus the old left side of the screen, minus the margin. If we didn't subtract the margin then only the right pixel of the character would be viewable, but, since the right margin is basically the padding we subtract it and everything works fine!