# Problem with drawing partially transparent tiles

Page 1 of 1

## 3 Replies - 892 Views - Last Post: 09 September 2012 - 10:25 AM

### #1 Natman

Reputation: 0
• Posts: 6
• Joined: 08-September 12

# Problem with drawing partially transparent tiles

Posted 08 September 2012 - 09:35 PM

```public int Alpha
{
get { return alpha; }
set
{
alpha = (int)MathHelper.Clamp(alpha, 0, 255);
}
}

public void Draw(SpriteBatch spriteBatch, Camera camera)
{
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

int tileMapWidth = map.GetLength(1);
int tileMapHeight = map.GetLength(0);

for (int y = 0; y < tileMapWidth; y++)
{
for (int x = 0; x < tileMapHeight; x++)
{
int textureIndex = map[x, y];

if (textureIndex == -1)
continue;

Texture2D texture = tileTextures[textureIndex];
spriteBatch.Draw(
texture,
new Rectangle(
y * tileWidth - (int)camera.Position.X,
x * tileHeight - (int)camera.Position.Y,
tileWidth,
tileHeight),
new Color(255, 255, 255, Alpha));
}
}

spriteBatch.End();
}

//in Game1.cs I make sure Alpha is set to the proper value with this line of code

tileLayer3.Alpha = 128;

```

This TileLayer class represents one layer of a larger TileMap. I'm trying to implement controlling the alpha level of an entire TileLayer, and drawing the layer using that partial alpha level. The TileLayer class contains an int alpha with a property restricting its values between 0 and 255.

For some reason, the TileLayer still draws at 100% opacity. When I replace Alpha in the Draw() method with the hard-coded value of 128, it draws at half transparency the way it should. But when I try to assign the Alpha of any one TileLayer instance from within Game1.cs, it simply draws at 100% opacity. I'm not sure what the problem could be, I've tried everything. If anything is unclear, I can answer any questions or post more of the code. Thanks!

Is This A Good Question/Topic? 0

## Replies To: Problem with drawing partially transparent tiles

### #2 LiberLogic969

Reputation: 34
• Posts: 85
• Joined: 03-September 12

## Re: Problem with drawing partially transparent tiles

Posted 09 September 2012 - 12:41 AM

Hello,

I think your problem is in the setter. You arnt using 'value' so the alpha variable is never actually changed.

```
public int Alpha
{
get { return alpha; }
set
{
value = (int)MathHelper.Clamp(value, 0, 255);
alpha = value;
}
}

```

Hope that helps

### #3 Natman

Reputation: 0
• Posts: 6
• Joined: 08-September 12

## Re: Problem with drawing partially transparent tiles

Posted 09 September 2012 - 08:45 AM

Thank you so much! Problem solved. I never would have looked in the property for the problem. I assumed it was with the Draw method, since I'm working from tutorials made in XNA 2.0 and apparently the protocol for drawing using Alpha has changed in 4.0. I assumed that was the problem.

### #4 LiberLogic969

Reputation: 34
• Posts: 85
• Joined: 03-September 12

## Re: Problem with drawing partially transparent tiles

Posted 09 September 2012 - 10:25 AM