Resizing graphic on load.

77 views
Skip to first unread message

Daniel Vazquez

unread,
Mar 1, 2016, 11:42:13 AM3/1/16
to HaxeFlixel
Hello! I'm here with a question today. The thing is I've been trying to resize several graphics on load, everything was going neat using the scale feature that FlxSprite provides within the next function:

private function fit() {
this.scale.set(Map.scale, Map.scale);
this.setSize(width * Map.scale, height * Map.scale);
this.centerOffsets(true);
}

And everything worked fine as it would resize both the graphic and it's corresponding bounding box to the given scale, however when I started using pixelPerfectOverlap I noticed some strange behavior when the scale changed. Upon skimming trough the code I discovered that pixelPerfectOverlap uses the frameWidth/Height properties both of which do not change size with the scale! So for what I understand is: The scaling is not happening on load but rather on draw, meaning that internally my sprites are still their original size and only change when drawn on screen every frame. If this is correct how can I do the scaling only on load? I don't need any type of dynamic scaling, I just need to specify a scale on load and have all the original assets be loaded to memory downsized. Thanks for your time!

Gama11

unread,
Mar 1, 2016, 12:21:17 PM3/1/16
to HaxeFlixel
You can use BitmapData#draw() with a transformation matrix for scaling.

Daniel Vazquez

unread,
Mar 2, 2016, 12:32:18 AM3/2/16
to HaxeFlixel
Thanks a lot! After a while struggling with understanding OpenFl's BitmapData I was able to do it. I ended up overwriting the loadGraphic function and adding this code at the beginning:

var newone:Bool = FlxG.bitmap.get(FlxG.bitmap.generateKey(Graphic, Key, Unique)) == null;
var graph:FlxGraphic = FlxG.bitmap.add(Graphic, Unique, Key);
if(newone){
var aux:BitmapData = new BitmapData(Std.int(graph.bitmap.width * 0.5), Std.int(graph.bitmap.height * 0.5), true, 0x00000000);
var mat:Matrix = new Matrix();
mat.scale(0.5, 0.5);
aux.draw(graph.bitmap, mat);
graph.bitmap = aux;
}

I'm not sure it it's the most optimal solution but it gets the work done and now pixelPerfectOverlap works flawlessly. 
Reply all
Reply to author
Forward
0 new messages