Answer the question
In order to leave comments, you need to log in
Do I understand the idea of scrolling the game world correctly?
Let us initially have a square (game window). There is a hero whose x position is equal to half the width of the game window. That is, the hero constantly stands in the middle, not moving. By pressing the buttons to the right or left, we thereby give an offset along the x-coordinate to our game. Accordingly, after that we shift all other objects as follows: someObj.x -= offsetX; (offsetX can be either negative or positive).
Am I speaking correctly? Is there a code example where this is all done correctly?
Answer the question
In order to leave comments, you need to log in
1. It is easier to change the coordinates of the Camera or Viewport object. And already on the go, when rendering, calculate someObject.x - viewport.x
.
After all, the display of objects is a small part of the game logic: objects need to live their own lives, interact with each other and the hero, and be cut off in advance if they are far from the camera.
And if - suddenly - there are several windows, for example, for playing together, you can make several viewports.
2. This is just the simplest scrolling algorithm (the so-called "attached camera"):
clamp(x, y, z) = max(min(x, z), y); for y>z, you can (but not necessarily) adjust your tricky behavior by taking, for example, (y + z) / 2.
If the game is dynamic and the viewport is small, you can complicate the camera behavior.
I have an excellent translation for this.
https://habrahabr.ru/post/272933/
https://habrahabr.ru/post/273397/
PS Platformers were written professionally, however, not on the HTML 5 canvas, but on Java ME.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question