0

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);
}
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91

1 Answers1

1

Look this Resizing BitmapData in ActionScript 3

You should use BitmapData.draw and scaled matrix.

Community
  • 1
  • 1
Sergey Miryanov
  • 1,820
  • 16
  • 29
  • Tried as in that. It now simply doesn't render at all – Sri Harsha Chilakapati Apr 29 '13 at 08:56
  • I use this method and all works fine. Can you show minimal compilable example? And also why not to add bitmap to stage - why you draw it with beginBitmapFill? – Sergey Miryanov Apr 29 '13 at 10:16
  • I'm actually porting an existing game made in Java to Haxe and I usually have a large asset and manually resize it according to the screen size of the user. Since every object needs to be animated, and I can't change the bitmap added to stage, I'm drawing it to the game class which is a sprite. – Sri Harsha Chilakapati Apr 29 '13 at 12:01