How to make a 2d scroller game

I really don’t understand how can I make a side scroller? How do I know what to do when my character moves? What positioning should I use for the characters? Hope my question is clear.

0
2d rendering
source share
3 answers

The easiest way I've found this is to have the variable characterX and characterY [integer or float, no matter what you want]. Then use the variable cameraX and cameraY. Each object in the scene is drawn in an ObjectX-CameraX object, object object Y ...

CameraX / CameraY are matched by a formula with a similar middle, so that eventually they will reach playerx / playery [Cx = (Cx*99+Px)/100] ... yeah

Thus, each object moves in the space of the scene and is converted only in the render [saves you from headaches]

+1
source share
  • Use the matrix to determine the reference frame of the camera.
  • Use split space to split the level into screens / windows.

Think of your gaming sprite like any other object, such as enemies and interactive objects. Now you need a camera abstraction. You can define the camera as a 3x3 matrix with this layout:

 [rotX_X, rotY_X, 0] [rotX_Y, rotY_Y, 0] [transX, transY, 1] 

The 2x2 submatter in the upper left corner is the rotation matrix. transX and transY defines the part of the translation, that is, the beginning. You also get scaling for free. Just simply scale the rotation part with a scalar, and you have a zoom.

For this to work correctly with rotation, your sprites must be polygons / primitives, such as triangles or squares; you cannot just apply the matrix to the positions of the sprites when drawing. If you do not need rotation, just the center point transformation will work fine. If you want the camera to follow the player, use the player’s position as the source of the camera. This is the translation vector [transX, transY] So, how do you apply the matrix to the positions of the entity and the vertices of the model? You are doing vector matrix multiplication. v '= vM ^ -1 , where v' is the new vector, v is the old vector, and M ^ -1 is the inverse matrix. The camera must be an inverse transformation, since it defines a local coordinate system. The analogy may be: if you are in front of me, and I turn to the left of my frame of reference, I turn to the right. This applies to all affine and linear transformations, such as scaling, rotation, and translation.

Divide your level into parts so that you can select objects and decorations that do not need to be visualized. Your viewport has a specific size / resolution. Only visualize the scenery and objects that intersect with your viewport. Instead of checking each object at the borders of the viewing area, assign each object to a specific sub-screen and check the borders of the sub-screen relative to the borders of the view and camera. If your dividing your levels into parts that are the same size as your viewport, then the maximum number of screens at any given time is displayed:

  • 2, if your camera only scrolls left and right.
  • 4, if your camera scrolls left, right, up and down.
  • 4, if your camera scrolls in any direction and can additionally be rotated.

Screen change is an event that you can use to activate objects belonging to this screen. It can be enemies, background animation, doors, or whatever you like.

0
source share

If this is your first foray into scroller recording, I would suggest considering using an existing game engine (like Construct or Gamemaker or XNA or whatever suits your experience level), so you don’t have to worry about what order to do things and how make it all work. A little with this - perhaps by studying some of them - in order to feel how they do it, then go to your home as soon as you get used to it. Not that there is anything wrong with the baptism of fire, but, in my opinion, it can become quite overwhelming.

0
source share

All Articles