Skip to content

Commit dcd3895

Browse files
authored
FlxGridOverlay: Prevent freeze when passing invalid cell size values (HaxeFlixel#462)
* FlxGridOverlay: Prevent freeze when passing invalid cell size values * Throw instead of returning null * Docs
1 parent e7350a9 commit dcd3895

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

flixel/addons/display/FlxGridOverlay.hx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class FlxGridOverlay
2222
* If false each row will be the same colour, creating a striped-pattern effect.
2323
* So to create an 8x8 grid you'd call create(8,8)
2424
*
25-
* @param CellWidth The grid cell width
26-
* @param CellHeight The grid cell height
27-
* @param Width The width of the FlxSprite. If -1 it will be the size of the game (FlxG.width)
28-
* @param Height The height of the FlxSprite. If -1 it will be the size of the game (FlxG.height)
25+
* @param CellWidth The grid cell width. Must be greater than 0.
26+
* @param CellHeight The grid cell height. Must be greater than 0.
27+
* @param Width The width of the FlxSprite. If -1 it will be the size of the game (FlxG.width). This value cannot be smaller than the `CellWidth`
28+
* @param Height The height of the FlxSprite. If -1 it will be the size of the game (FlxG.height). This value cannot be smaller than the `CellHeight`
2929
* @param Alternate Should the pattern alternate on each new row? Default true = checkerboard effect. False = vertical stripes
3030
* @param Color1 The first fill colour in 0xAARRGGBB format
3131
* @param Color2 The second fill colour in 0xAARRGGBB format
@@ -45,9 +45,10 @@ class FlxGridOverlay
4545
}
4646

4747
if (Width < CellWidth || Height < CellHeight)
48-
{
49-
return null;
50-
}
48+
throw "The width and height of a grid cannot be smaller than the cell's width and height!";
49+
50+
if (CellWidth <= 0 || CellHeight <= 0)
51+
throw "Grid cell width/height cannot be less or equal to 0";
5152

5253
var grid:BitmapData = createGrid(CellWidth, CellHeight, Width, Height, Alternate, Color1, Color2);
5354

@@ -67,10 +68,10 @@ class FlxGridOverlay
6768
* So to create an 8x8 grid you'd call create(8,8,
6869
*
6970
* @param Sprite The FlxSprite you wish to draw the grid on-top of. This updates its pixels value, not just the current frame (don't use animated sprites!)
70-
* @param CellWidth The grid cell width
71-
* @param CellHeight The grid cell height
72-
* @param Width The width of the FlxSprite. If -1 it will be the size of the game (FlxG.width)
73-
* @param Height The height of the FlxSprite. If -1 it will be the size of the game (FlxG.height)
71+
* @param CellWidth The grid cell width. Must be greater than 0.
72+
* @param CellHeight The grid cell height. Must be greater than 0.
73+
* @param Width The width of the FlxSprite. If -1 it will be the size of the game (FlxG.width). This value cannot be smaller than the `CellWidth`
74+
* @param Height The height of the FlxSprite. If -1 it will be the size of the game (FlxG.height). This value cannot be smaller than the `CellHeight`
7475
* @param Alternate Should the pattern alternate on each new row? Default true = checkerboard effect. False = vertical stripes
7576
* @param Color1 The first fill colour in 0xAARRGGBB format
7677
* @param Color2 The second fill colour in 0xAARRGGBB format
@@ -90,9 +91,10 @@ class FlxGridOverlay
9091
}
9192

9293
if (Width < CellWidth || Height < CellHeight)
93-
{
94-
return null;
95-
}
94+
throw "The width and height of a grid cannot be smaller than the cell's width and height!";
95+
96+
if (CellWidth <= 0 || CellHeight <= 0)
97+
throw "Grid cell width/height cannot be less or equal to 0";
9698

9799
var grid:BitmapData = createGrid(CellWidth, CellHeight, Width, Height, Alternate, Color1, Color2);
98100
Sprite.pixels.copyPixels(grid, new Rectangle(0, 0, Width, Height), new Point(0, 0), null, null, true);

0 commit comments

Comments
 (0)