-2

I'm new at this and I'm making a very simple 2D platformer with a level is loaded from a text file. I'm having trouble to try how to figure out how I can have my character sprite collide with the tiles. This is an example of a .txt that my game will run:

1,1,1,1,1,1,1,1,
1,1,1,0,0,0,0,1,
1,1,0,0,0,1,0,1,
1,0,0,0,0,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,1,0,0,1,
1,S,0,1,1,0,F,1,
1,1,1,1,1,1,1,1,

I've managed to figure out how to successfully draw them onto the page, but cannot progress further. My current model has my character colliding with the edge of the screen by having a boolean for Collision equal to true when the edge of the sprite touches the edge of the screen and setting its velocity to 0, but I'm not sure how to do that to block that are generated from a .txt My current model of drawing a level looks like this:

if (currentState == GameState.Playing)
                {
                    for (y = 0; y <= 7; y++)
                    {
                        string[] mapchars = maplines[y].Split(','); //where maplines is each row of characters and mapchars is each individual character//
                        for (x = 0; x <= 7; x++)
                        {

                            if (mapchars[x] == "1")
                            {
                                spriteBatch.Begin();
                                spriteBatch.Draw(block, new Vector2((200 + (50 * x)), ((50 * y))), Color.White);
                                spriteBatch.End();
                            }
                            if (mapchars[x] == "F")
                            {
                                spriteBatch.Begin();
                                spriteBatch.Draw(block, new Vector2((200 + (50 * x)), ((50 * y))), Color.Yellow);
                                spriteBatch.End();
                            }
                            if (mapchars[x] == "S")
                            {
                                spriteBatch.Begin();
                                spriteBatch.Draw(sprite, new Rectangle((200 + (50 * x)), ((50 * y)), 40, 40), Color.White*0.5f);
                                spriteBatch.End();
                            }
                            if (x > 7)
                            {
                                break;
                            }
                        }
                        if (y > 7)
                        {
                            break;
                        }
                    }
                }

Where maplines[] is each line of the .txt and where mapchars[] is for each individual character on each line. If anyone knows a solution to this problem, it would be a great help :) Thanks.

  • possible duplicate of [Tile map Collision Detection](http://stackoverflow.com/questions/9916616/tile-map-collision-detection) –  Feb 02 '15 at 04:22

1 Answers1

1

Don't do that like this ! You are wasting a lot of speed with a game coded like this. I have an issue for you.

  1. First, get all your .txt map into a string[] wich is line is an string. Do that with the method File.ReadAllLines(string yourtxtfilepath).
  2. Once you have done this, create an new 2 dimensional array of Block (Block[,]). You have to create an general abstract class Block wich all your types of blocks (rock, air, grass ...) inherits from its.
  3. Create a for loop who checks all chars in all the string of the array of string you got. In that loop check if the character is an '1' or an 'F' or something else by a case statement. For example, if it was an 'F' add a new GrassBlock to your Block 2 dimensional array.
  4. Finally, after the loop you get your map in your 2 dimensional Block array, wich is perfect to get access to all your blocks in the map.
  5. Just put a foreach loop in the Draw() method to draw each block to your screen by incrementing their positions by the width and the heights of your tiles.

If you don't understand what I said, just do some Google searches to get the necessary knowledge so you will understand. Start by acknowledge what I try to explain you and after, you can concentrate yourself on collisions, wich is more advanced.