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



所以我正在使用Pushbutton Engine (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创造基于贴图的游戏。

虽然我确信我能拼凑出一些东西来做这件事,但我想确保我用的是正确的方法,这样我就能得到最好的表现。有什么提示我应该在哪里寻找这个标准效应吗?

谢谢,布拉德。

稍微摆弄一下,结果就很简单了。

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));
        }
    }
}

color me brad在这方面做了很好的尝试,这让我开始思考这个问题。

重写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();
        }
    }
}

最新更新