我正在尝试用 haxe 学习 NME 来创建一个小游戏。我在 FlashDevelop 中设置了 NME 3.5.5 和 Haxe 2.10。为了绘制游戏背景,我正在使用
// Class level variable
var background : nme.display.Bitmap;
public function initResources() : Void
{
background = new Bitmap(Assets.getBitmapData("img/back.png"));
}
在渲染循环中,我是这样渲染的。
g.clear();
g.beginBitmapFill(background.bitmapData, true, true);
g.drawRect(0, 0, 640, 480);
g.endFill();
这是在视图中绘制图像,我需要调整它的大小以适应屏幕。
编辑:
这是我用来缩放位图的函数。它不起作用,屏幕上没有呈现任何内容。
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);
}
谢谢。
编辑2:
终于成功了。我不必要地将它转换为int。这是解决方案。
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);
}