Jump to content
  • Advertisement
Sign in to follow this  
D6Gamer

Help refactoring function into a class

This topic is 3213 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi As the title says I'm trying to re-factor one of my functions found in my Load() (i'm using xna). The problem is, I have never re factored before. What I want to change into a class is this:
//variables
public Sprite tile;
        public const int maxTiles = 100;
        string[] tileMap = new string[maxTiles];

        int y;
        int x;

        List<Sprite> tiles;

//code in loadcontent function
            using (StreamReader sr = new StreamReader("level01.txt"))
            {
                int x = 0;
                int y = 0;
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] elements = line.Split(',');

                    foreach (string e in elements)
                    {
                        int type = int.Parse(e);

                       //load sprites according to textfile
                        if (type == 1)
                        {
                            tile = new Sprite();
                            tile.Position = new Vector2(x, y);
                            tile.Load("brickwall", Content, simulator);
                            tile.IsStatic = true;
                            tile.Geom.OnCollision += OnCollision;
                            tiles.Add(tile);
                            tile.Geom.FrictionCoefficient = 0.5f;


                        }

                        if (type == 2)
                        {
                            playerSprite = new Player();
                            playerSprite.Position = new Vector2(x, y);
                            playerSprite.Load("Player", Content, simulator);
                            playerSprite.Geom.OnCollision += OnCollision;
                            playerSprite.Body.Mass = 5;
                            tiles.Add(playerSprite);

                        }
                        if (type == 3)
                        {
                            tile = new Sprite();
                            tile.Position = new Vector2(x, y);
                            tile.Load("box", Content, simulator);
                            tile.IsStatic = true;
                            tile.Geom.OnCollision += OnCollision;
                            tiles.Add(tile);
                            tile.Geom.FrictionCoefficient = 0.5f;


                           

                        }
                       //tile width
                        x += 25;
                    }
                    x = 0;
                   //tile height
                    y += 25;

                }

            }

//code in draw function

            foreach (Sprite sprite in tiles)
            {
                if (sprite.GetType() != playerSprite.GetType())
                {
                    spriteBatch.Draw(sprite.CurrentTexture, sprite.Position, null, Color.White, sprite.Rotation, sprite.Origin, 1, SpriteEffects.None, 1);
                }
                     if (playerSprite.Body.LinearVelocity.X >= 0)
               {
                   spriteBatch.Draw(playerSprite.CurrentTexture, playerSprite.Position, null, Color.White, playerSprite.Rotation, playerSprite.Origin, 1, SpriteEffects.None, 1);
                }
                if (playerSprite.Body.LinearVelocity.X < 0)
                {
                   spriteBatch.Draw(playerSprite.CurrentTexture, playerSprite.Position, null, Color.White, playerSprite.Rotation, playerSprite.Origin, 1, SpriteEffects.FlipHorizontally, 1);
                }
            }


Any help in this would be great.

Share this post


Link to post
Share on other sites
Advertisement
I'm confused. Which function are you trying to refactor? Where are those variables? When posting functions, it's best to post the whole declaration, including the name and arguments.

FWIW, refactoring is not some special process. If you've ever moved a function into/out of a class, pulled a base class out of another class so you could derive other classes from it, or any structural change like that, you've done at least a form of refactoring.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!