1
votes

I'm trying to create a sprite sheets from a movie clips, and then scale it depending of the resolution of the screen. Also, I need to know the exact height and width of the frame. Here is the code I use : (NOTE: BimapWithDimension is a custom class that I made, for storing the bitmap data and the width and height; _m - is a matrix)

private function createSpriteSheet(mc:Class):BimapWithDimension
{
        var _bD:BimapWithDimension = new BimapWithDimension();

        var _mc:MovieClip = new mc();
        _mc.gotoAndStop(1);

        var _frejmoviVoRed:int = _mc.totalFrames < 10 ? _mc.totalFrames : 10;
        var _redovi:int = Math.ceil(_mc.totalFrames/_frejmoviVoRed);
        var _h:int = _mc.height * _redovi;

        _bmp = new BitmapData((_mc.width * _frejmoviVoRed) * _appScale, _h * _appScale, true, 0x00000000);

        var _sirina:int = Math.ceil(_mc.width * _appScale);
        var _visina:int = Math.ceil(_mc.height * _appScale);

        var _bmp1:BitmapData = new BitmapData(_sirina, _visina);
        var _rect:Rectangle = new Rectangle(0, 0, _sirina, _visina);

        for (var i:uint = 0; i < _mc.totalFrames; i++)
        {
            _bmp1.fillRect(_rect, 0x000000);
            _m.identity();
            _m.scale(_appScale, _appScale);
            _bmp1.draw(_mc, _m );

            _m.identity();
            _m.translate(_sirina * (i % _frejmoviVoRed), _visina * Math.floor(i / _frejmoviVoRed));
            _bmp.draw(_bmp1, _m);
            _mc.nextFrame();
        }

        _bD.bmp = _bmp; 
        _bD.width = _sirina;
        _bD.height = _visina;

        return _bD;
}

Now the problem is that the dimensions on some resolutions are wrong. E.g. on a resolution of 480x320 some sheets don't have the same width, that is the width that is stored and actual drawn width. I'm guessing that is due to rounding the values, but no matter how I try to correct this (Using casting to int, Math.floor() or Math.ceil()), I cannot.

So, how to find out the real width and height ?

EDIT: I found a ... let's say a semi solution. I introduced another BitmapData, that is scaled to a round number, than draw the moviclip's frame on it(scaled), and than that object is drawn on the sprite sheet. I updated the function with the new code. If someone has another solution, I would really like to know.

1
Rounding will ever at the most only be 1 or 2 pixels off never more. Don't use a matrix when drawing into the bmp. This will always give funny results.The_asMan
Once you have a BitmapData, you may use copyPixels() to transfer it into a sprite sheet. Otherwise, it's a good solution.Vesper

1 Answers

0
votes

Have a look to this, this class allow you to transform a movieclip into a bitmap spritesheet

https://github.com/emibap/Dynamic-Texture-Atlas-Generator/blob/master/dynamicTextureAtlasGenerator/src/com/emibap/textureAtlas/DynamicAtlas.as

This class output to starling texture altlas but it require minor change to obtain a regular bitmapData.

Another way is to use flash cs6 > right click on your movieclip in the library and select 'convert to sprtiesheet'