Hey,
I’m currently attempting to add in map scrolling to a basic platformer I’m working on. I worked out a good idea of how it works, but every time I try to implement map scrolling it never works properly.
The player is 32 pixels wide and 64 pixels tall.
///////// WIP - Screen scrolling code.
double playerX = ((Player)ENTITIES.get(0)).getX();
double halfScreenWidth = (SCREEN_DIMENSIONS.getWidth()/2);
if(playerX > halfScreenWidth) {
double distanceFromHalfScreenWidth = playerX - halfScreenWidth;
offsetX -= distanceFromHalfScreenWidth;
((Player)ENTITIES.get(0)).setX(halfScreenWidth - distanceFromHalfScreenWidth); // Rest works, no idea what to set X to atm.
} else {
offsetOldX = offsetX;
}
/////// End of WIP
This is what I’m currently using. It does scroll the map to the left, but it does this:
I’ve modified the code above in many ways, but only the current code has resulted in anything close to working properly. The screen seems to scroll too far and the player is often thrown inside of a tile where it’s not allowed to move. If anyone has an idea of what I may have messed up on in the code that would help a lot.