Help refactoring function into a class

Started by
0 comments, last by theOcelot 14 years, 2 months ago
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.
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.

This topic is closed to new replies.

Advertisement