按钮引擎 - 跨精灵重复/平铺艺术资产

时间:2011-04-09 13:41:48

标签: actionscript-3 sprite

所以我正在使用按钮引擎(http://www.pushbuttonengine.com)创建一个经典的侧卷轴,我希望'世界'位是任何给定的大小,当然,重复/平铺我的艺术资产。我有以下渲染设置代码作为开始:

var Render:SpriteRenderer = new SpriteRenderer();                                                                            
Render.positionProperty = new PropertyReference("@Spatial.position");
Render.sizeProperty = new PropertyReference("@Spatial.size");
Render.fileName = "./media/brick.png";
Render.scene = PBE.scene;
Render.layerIndex = 1;

然而,这只需要brick.png并拉伸/缩小它以填充对象的大小。瘸。 :)我花了很多时间试图谷歌如何做到这一点,但我可能没有得到正确的条款或一些像我没有想出任何东西。主要是关于使用PBE构建基于平铺的游戏的文章。

虽然我确信我可以将可以解决问题的东西放在一起,但我想确保我以正确的方式做到这一点,以便获得最佳性能,而不是。关于我应该在哪些方面提取这种标准效果的任何提示?

感谢, 布拉德

2 个答案:

答案 0 :(得分:1)

有些不知所措,解决方案非常简单。

public class TiledBitmapRenderer extends BitmapRenderer
{       
    public function TiledBitmapRenderer(bitmap:Bitmap, width:int, height:int)
    {
        super();

        positionOffset = new Point(width/-2,height/-2);

        var cx:int = Math.ceil(width/bitmap.width);
        var cy:int = Math.ceil(height/bitmap.height);
        bitmapData = new BitmapData(width, height, false, 0x000000);

        // fill the bitmapData object with all display info with the provided bitmap 
        for (var ix:int = 0; ix<cx; ix++) {
            for (var iy:int = 0; iy<cy; iy++)
                bitmapData.copyPixels(bitmap.bitmapData,bitmap.bitmapData.rect, new Point(ix*bitmap.width,iy*bitmap.height));
        }
    }
}

答案 1 :(得分:1)

颜色我布拉德对此做了很好的尝试 - 这让我想到了这个问题。

重写BitmapRenderer以允许平铺很棘手,因为渲染器组件所做的更改是使用矩阵执行的。我编写了一个忽略缩放的类,而是根据需要创建一个具有更大物理尺寸的新位图。

下面的解决方案确实带来了一些小的限制:我必须完成缩放更改并检查小数点后1位的变化(测试证明这对于特定比例的物体有时是必要的)。

其次,因为我们每次重绘时都会重绘,所以它可能不是很好地优化,好像我们只是更新矩阵比例。

尽管如此 - 它仍然非常快,对于PBE的“哲学”是真实的,我现在正在游戏中愉快地使用它。

package render
{
    import com.pblabs.engine.PBUtil;
    import com.pblabs.rendering2D.BitmapRenderer;
    import com.pblabs.rendering2D.SpriteRenderer;

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.geom.Vector3D;

    /**
     * Creates a tiles bitmap. The tile is identified as the first bitmap parameter set when this component is created.
     */
    public class TileRenderer extends BitmapRenderer
    {
        public function TileRenderer()
        {

            super();
        }

        protected var lastScaleX:Number = undefined;
        protected var lastScaleY:Number = undefined;

        // the original bitmap contains the single tile we will reuse to fill out the bitmap
        protected var originalBitmapData:BitmapData;


        /**
         * Just set this to the tile you want to reuse. It will automatically figure out tile width and height.
         */
        override public function set bitmapData(value:BitmapData):void{


            if (value === bitmapData)
                return;

            originalBitmapData = value;

            bitmap.bitmapData = advanceRender(value.width, value.height);

            // Due to a bug, this has to be reset after setting bitmapData.
            smoothing = _smoothing;

            // set registration point to get spritesheet-component-style centering
            registrationPoint = new Point(value.width/2,value.height/2);    

            _transformDirty = true;


        }

        /**
         * tile the original bitmap across a new bitmap area
         * the following is adapted from http://stackoverflow.com/users/102373/colour-me-brad:
         */
        protected function advanceRender(targetWidth:Number, targetHeight:Number):BitmapData{

            var target:BitmapData = new BitmapData(targetWidth,targetHeight, true,0x00000000);

            var cx:int = Math.ceil(target.width/originalBitmapData.width);
            var cy:int = Math.ceil(target.height/originalBitmapData.height);

            target = new BitmapData(target.width, target.height, true, 0x000000);


            // fill the bitmapData object with all display info with the provided bitmap 
            for (var ix:int = 0; ix<cx; ix++) {
                for (var iy:int = 0; iy<cy; iy++)
                    target.copyPixels(originalBitmapData,originalBitmapData.rect, new Point(ix*originalBitmapData.width,iy*originalBitmapData.height));
            }
            return target;
        }


        /**
         * heavily override this function to avoid changing scale. We instead redraw the 
         * bitmap to the correct width and height.
         */
        override public function updateTransform(updateProps:Boolean = false):void
        {

            if(!displayObject)
                return;

            if(updateProps)
                updateProperties();

            // If size is active, it always takes precedence over scale.
            var tmpScaleX:Number = _scale.x;
            var tmpScaleY:Number = _scale.y;
            if(_size)
            {
                var localDimensions:Rectangle = displayObject.getBounds(displayObject);
                tmpScaleX = _scale.x * (_size.x / localDimensions.width);
                tmpScaleY = _scale.y * (_size.y / localDimensions.height);
            }


            _transformMatrix.identity();
            //_transformMatrix.scale(tmpScaleX, tmpScaleY);
            _transformMatrix.translate(-_registrationPoint.x * tmpScaleX, -_registrationPoint.y * tmpScaleY);
            _transformMatrix.rotate(PBUtil.getRadiansFromDegrees(_rotation) + _rotationOffset);
            _transformMatrix.translate(_position.x + _positionOffset.x, _position.y + _positionOffset.y);


            if (getRoundDecimal(tmpScaleX,1) != 1 || getRoundDecimal(tmpScaleY,1) != 1) {


                bitmap.bitmapData = advanceRender(originalBitmapData.width*tmpScaleX, originalBitmapData.height * tmpScaleY);
                smoothing = _smoothing;

                // set registration point to get spritesheet-component-style centering
                registrationPoint = new Point(bitmap.bitmapData.width/2,bitmap.bitmapData.height/2);

            }

            displayObject.transform.matrix = _transformMatrix;
            displayObject.alpha = _alpha;
            displayObject.blendMode = _blendMode;
            displayObject.visible = (alpha > 0);


            _transformDirty = false;

        }


        /**
         * taken from: http://swordfish1987.wordpress.com/2009/07/10/decimal-rounding-in-actionscript-3/
         */
        public static function getRoundDecimal(num:Number, precision:int):Number{

            var decimal:Number = Math.pow(10, precision);

            return Math.round(decimal* num) / decimal;

        }

        override protected function onRemove():void{
            originalBitmapData = null;
            super.onRemove();
        }
    }
}