imagehaxenme

Haxe NME Resizing a Bitmap


I am trying to learn NME with haxe to create a small game. I have setup NME 3.5.5 with Haxe 2.10 in FlashDevelop. To draw the game background, I'm using

// Class level variable
var background : nme.display.Bitmap;

public function initResources() : Void
{
    background = new Bitmap(Assets.getBitmapData("img/back.png"));
}

And in the render loop, I'm rendering like this.

g.clear();
g.beginBitmapFill(background.bitmapData, true, true);
g.drawRect(0, 0, 640, 480);
g.endFill();

This is drawing the image across the view and I need to resize it to fit the screen.

EDIT:

Here is the function i'm using to scale the bitmap. It doesn't work and nothing is rendered on the screen.

public static function resize( source:Bitmap, width:Int, height:Int ) : Bitmap
{
    var scaleX:Int = Std.int(width / source.bitmapData.width);
    var scaleY:Int = Std.int(height / source.bitmapData.height);
    var data:BitmapData = new BitmapData(width, height, true);
    var matrix:Matrix = new Matrix();
    matrix.scale(scaleX, scaleY);
    data.draw(source.bitmapData, matrix);
    return new Bitmap(data);
}

Thanks.

EDIT 2:

Finally made it. I was unnecessarily casting it to int. Here's the solution.

public static function resize( source:Bitmap, width:Int, height:Int ) : Bitmap
{
    var scaleX:Float = width / source.bitmapData.width;
    var scaleY:Float = height / source.bitmapData.height;
    var data:BitmapData = new BitmapData(width, height, true);
    var matrix:Matrix = new Matrix();
    matrix.scale(scaleX, scaleY);
    data.draw(source.bitmapData, matrix);
    return new Bitmap(data);
}

Solution

  • Look this Resizing BitmapData in ActionScript 3

    You should use BitmapData.draw and scaled matrix.