I have a function that sets a draw-state for a specific tile given another tile. The tile that's draw state is going to change compares tiles that are surrounding it and then updates accordingly. I'll try to illustrate that below
[b] [b] [a]
[b] [a] [a]
[a] [a] [a] where a = sand && b = water
when a detects that b is bordering it, it must update its draw-state. So I have a function that works for an upper case, lower case, left case, and right case. I now need to modify that function so it can handle a left-right case, upper-right case, lower-right case, etc. etc. Here is my function
public override void CompareBorderingTiles(Tile T)
{
if (T is Water)
{
float leftBound = location.X - (Tile.TileWidth * Tile.TileScale);
float rightBound = location.X + (Tile.TileWidth * Tile.TileScale);
float upperBound = location.Y - (Tile.TileHieght * Tile.TileScale);
float bottomBound = location.Y + (Tile.TileHieght * Tile.TileScale);
if (T.GridLocation.X == leftBound)
{
drawstate = DrawState.Left;
}
if (T.GridLocation.X == rightBound)
drawstate = DrawState.Right;
if (T.GridLocation.Y == upperBound)
drawstate = DrawState.Upper;
if (T.GridLocation.Y == bottomBound)
drawstate = DrawState.Lower;
}
base.CompareBorderingTiles(T);
}
It should be pretty explanatory as to why i'd want to break out of this function, or maybe not. Basically I have an enum which tells me what my draw-state is (drawstate is the enum). Can anybody tell me if I can set that correct draw state and then get out of my function?
Use the return keyword to exit from a method. public void someMethod() { //... a bunch of code ... if (someCondition()) { return; } //... otherwise do the following... }
Using return is the easiest way to exit a function. You can use return by itself or even return a value.
Just use a return statement where you want to end:
return;
So, in your code you could do:
public override void CompareBorderingTiles(Tile T)
{
if (T is Water)
{
float leftBound = location.X - (Tile.TileWidth * Tile.TileScale);
float rightBound = location.X + (Tile.TileWidth * Tile.TileScale);
float upperBound = location.Y - (Tile.TileHieght * Tile.TileScale);
float bottomBound = location.Y + (Tile.TileHieght * Tile.TileScale);
if (T.GridLocation.X == leftBound)
{
drawstate = DrawState.Left;
return;
}
if (T.GridLocation.X == rightBound)
{
drawstate = DrawState.Right;
return;
}
if (T.GridLocation.Y == upperBound)
{
drawstate = DrawState.Upper;
return;
}
if (T.GridLocation.Y == bottomBound)
{
drawstate = DrawState.Lower;
return;
}
}
base.CompareBorderingTiles(T);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With