1
\$\begingroup\$

I'm experiencing a problem with my simple 2d tile map game. I have a character on screen that moves in all directions in a world made up of tiles/blocks. Basically I have created an array that with numbers are supposed to represent different type of blocks that in turn represent ground, sky and clouds. It helps when creating the map.

This array for context:

int map[10][30] =
{
    {2, 3, 2, 3, 3, 3, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 2, 2, 2, 2},
    {2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2},
    {2, 2, 2, 3, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 2, 2, 2},
    {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

Esentially what I want to do is that I want to move all blocks in the opposite direction of where the player is walking so a view is created. Now I don't want to use the pre-built in view functionality, I want to program my own view, which has led me to some problems.

I've tried with this code as an example to see if my idea would work:

for (int mapY = 0; mapY < 10; mapY++)
    {
        for (int mapX = 0; mapX < 30; mapX++)
        {
            if (map[mapY][mapX] == 1)
            {
                ground.setPosition(mapX * 50, mapY * 50);
                ground.move(-300, 0);
                window.draw(ground);
            }
            else if (map[mapY][mapX] == 2)
            {
                sky.setPosition(mapX * 50, mapY * 50);
                sky.move(-300, 0);
                window.draw(sky);
            }
            else if (map[mapY][mapX] == 3)
            {
                cloud.setPosition(mapX * 50, mapY * 50);
                cloud.move(-300, 0);
                window.draw(cloud);
            }
        }
    }

The objects in the code sky, ground, and cloud are RectangleShapes with the size 50 * 50 differentiated by different colors.

This code works for printing out the world, but for some reason i'm not moving the blocks at all. According to my logic this code would besides printing out the world also move it (all the blocks that is) for every frame. If I can make this work, I could replace the current move vector that only moves the world in one single direction, with a view_direction vector that changes depending on the state of the character (still, moving left, moving right etc). Anyone who knows what I'm doing wrong here or who has a better more efficient and easier idea of how to create your own view that would work in this environment.

\$\endgroup\$
  • \$\begingroup\$ My first piece of advice is to never name a class map \$\endgroup\$ – bruglesco Apr 24 at 1:54
  • \$\begingroup\$ If I understand corectly you want your tiles to move continuously like the old school sidescrollers, is that correct? \$\endgroup\$ – bruglesco Apr 24 at 1:54
  • \$\begingroup\$ @bruglesco yes indeed! \$\endgroup\$ – Felix Nilsson Apr 25 at 15:25
  • \$\begingroup\$ How is the above code being called. Do you have it in your game loop? \$\endgroup\$ – bruglesco Apr 26 at 2:10
  • \$\begingroup\$ @bruglesco yes it is \$\endgroup\$ – Felix Nilsson Apr 26 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.