Question

I currently have an issue regarding local variables in a method. The method is made to spawn a line of square sprites along the bottom of the screen.The code currently looks like so:

 public void spawnFloor()
    {
        for (int x = 0; x < floorSize / texture.Width; x++)
        {
            if (walls.Count < floorSize / texture.Width)
            {
                positionModifier.X = texture.Width * x;
                position.Y = floorStart.Y;
                spawnWall();
            }
        }
    }

Currently, when the game resolution is 1280/720, the game spawns 40 squares at the bottom right corner of the screen. The value of x is apparently 0 throughout. Any ideas? Below is the code of the class in it's entirety in case it's something class based:

    public WallManager(Texture2D texture, Vector2 floorStart, int screenLength)
    {
        this.texture = texture;
        this.floorStart = floorStart;
        floorSize = screenLength;
    }

    public void spawnWall()
    {
        Wall thisWall = new Wall(position*positionModifier, texture);
        walls.Add(thisWall);
    }

    public void spawnTest()
    {
        if (walls.Count == 0)
        {
            position = new Vector2(0, 698);
            spawnWall();
        }
    }

    public void spawnFloor()
    {
        for (int x = 0; x < floorSize / texture.Width; x++)
        {
            if (walls.Count < floorSize / texture.Width)
            {
                positionModifier.X = texture.Width * x;
                position.Y = floorStart.Y;
                spawnWall();
            }
        }
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        foreach(Wall wall in walls)

        wall.Draw(spriteBatch);
    }
}

Edit: As suggested, the wall class has been posted:

public Wall(Vector2 position, Texture2D texture)
    {
        wallPosition = position;
        wallTexture = texture;
    }

    public void Update(GameTime gameTime)
    {
        wallWidth = (int)Math.Round((double)wallTexture.Width);
        wallHeight = (int)Math.Round((double)wallTexture.Height);
        wallHitbox = new Rectangle((int)wallPosition.X, (int)wallPosition.Y, wallWidth, wallHeight);
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(wallTexture, wallPosition, wallColor);
    }

Also, positionModifier is a private Vector2 in the wallManager class with the value (1,1) on startup

Was it helpful?

Solution

Its not possible to tell whats broken based on the code you have posted. The issue is most likely in how Wall is being created. I would bet that either positionModifier.X does not actually do anything to the x position of the wall, or that the starting value for the walls x position is 0 and when you multiply positionModifier with it in the method call, it remains 0.

OTHER TIPS

Because it is doing Closures inside your loop. Copy a local variable for the value of x and store it as localVal (example) and then use that one when calculating your positionModifier

public void spawnFloor()
{
    for (int x = 0; x < floorSize / texture.Width; x++)
    {
        if (walls.Count < floorSize / texture.Width)
        {
            var localVal = x;
            positionModifier.X = texture.Width * localVal;
            position.Y = floorStart.Y;
            spawnWall();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top