Haxe NME调整位图大小 [英] Haxe NME Resizing a Bitmap
问题描述
我正在尝试用haxe来学习NME来创建一个小游戏。我在FlashDevelop中使用Haxe 2.10设置了NME 3.5.5。为了绘制游戏背景,我正在使用
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.
编辑:
这是我的功能m用于缩放位图。它不起作用,屏幕上没有任何内容。
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);
}
谢谢。
编辑2:
EDIT 2:
最后成功了。我不必要地将它转换为int。这是解决方案。
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);
}
推荐答案
看这个在ActionScript 3中重新调整BitmapData
你应该使用BitmapData.draw和缩放矩阵。
You should use BitmapData.draw and scaled matrix.
这篇关于Haxe NME调整位图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!