I'm in the process trying to add parallax scrolling to my game engine, however I cant quite get it working right, I've had results that seem to be close but there's the problem with either the tiles drifting off screen or the tiles don't offset properly.

I don't set the Z of the layers manually the layers movement speed should be based on how wide the layer is relative to the actual stage width, I've read several forum posts on the subject and several articles on Parallax scrolling and this is what i have so far (with some messy code as its still in the process of being figured out properly). I've left in some commented out code that i experimented with

the Stage class chooses the layers to be rendered, the Scene class tells the screen class which sprite and position to render it at offset by the viewport camera.

I've been focusing on making it work along the X axis first so the Y axis code is very preliminary.

I hope my code is descriptive enough

Also some of the code that was originally in the Stage Class has been moved to Scene so if you're wondering why there's some duplicate code, that's why

Also my code is inspired by the methods The Cherno and VanZeben (on youtube) use

sprites are currently 32x32

and any shifts by 5 is reflective of that size. (aka * or / 32)

and shiftX and shiftY are int == 5

also i forgot to mention

width and height = amount of tiles x and ytilemap.getWidth and getHeight are also amount of tiles;

viewport width and height are in pixels

Edit:

I'm cleaning up my code and i'll repost once done, i'm also re-looking over this reference material which is the formula for parallax i'm using

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org