Haxe NME Resizing a Bitmap

送分小仙女□ 提交于 2019-12-12 02:56:41

问题


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);
}

回答1:


Look this Resizing BitmapData in ActionScript 3

You should use BitmapData.draw and scaled matrix.



来源:https://stackoverflow.com/questions/16273440/haxe-nme-resizing-a-bitmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!