在平铺背景上创建视差

时间:2010-02-12 22:33:17

标签: flex flash actionscript-3 actionscript

我正在使用来自8bitrocket.com的Jeff用来创建磁贴的blitting技术。我想在bitmapdata上绘制2层bitmap。第一层是背景(1图像)。第二层是瓷砖。在下面的那个班里。 updateMap是在循环中调用以重新绘制图像的方法。

package com.eapi 
{
    /**
     * ...
     * @author Anthony Gordon
     */
    import com.objects.XmlManager;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.display.BitmapData;
    import flash.display.Bitmap
    import flash.geom.Rectangle;
    import flash.geom.Point;
    import flash.display.DisplayObject;

    public class EngineApi extends MovieClip
    {
        public var images:Array;
        public var world:Array;
        //w stands for world, how big it is in columns and rows
        private var wCols:Number = 50;
        private var wRows:Number = 16;

        public var wWidth:Number;
        public var wHeight:Number;
        //v stands for view, which means your field of view
        public var vRows:Number;
        public var vCols:Number;

        public var vWidth:Number = 540;
        public var vHeight:Number = 360;
        //how big your indivual tile is
        public var tileW:Number = 80;
        public var tileH:Number = 80;

        public var offsX:Number = 0;
        public var offsY:Number = 0;
        public var xEnd:Number = 0;
        public var yEnd:Number = 0;
        public var tilex:int;
        public var tiley:int;

        public var scrollx:Number = 0;
        public var scrolly:Number = 0;

        private var screen:Bitmap;
        private var canvas:BitmapData;
        private var buffer:BitmapData;
        public var mapHolder:Array;
        private var scrollLoop:Boolean;

        private var minLoop:Number;
        private var maxLoop:Number;

        public var currentMap:Number = 0;
        private var queue:Array;

        public var currentCol:Number = 0;
        public var currentRow:Number = 0;
        public var enviroment:Array;
        public var currentTileSheet:Number = 0;

        private var layer1:Sprite;
        private var layer2:Sprite;
        private var layer3:Sprite;
        private var layer4:Sprite;
        private var layer5:Sprite;

        public var background:BitmapData

        protected var stageObject:Array;
        protected var gameObjects:Array;

        public function EngineApi(w:Number = 540,h:Number = 360, tw:Number = 50, th:Number = 50) 
        {

            stageObject = new Array();
            gameObjects = new Array();

            //Add Layers
            layer1 = new Sprite();
            layer2 = new Sprite();
            layer3 = new Sprite();
            layer4 = new Sprite();
            layer5 = new Sprite();
            //end

            images = new Array();
            vWidth = w;
            vHeight = h;
            tileW = tw;
            tileH = th;
            queue = new Array();

            mapHolder = new Array();

            vCols = Math.floor(vWidth/tileW);
            vRows = Math.floor(vHeight/tileH);

            wWidth = wCols * tileW;
            wHeight = wRows * tileH;

            canvas = new BitmapData(vWidth,vHeight,true,0x000000);
            buffer = new BitmapData(vWidth + 2 * tileW, vHeight + 2 * tileH ,false,0x000000);           
            screen = new Bitmap(canvas);

            addChild(screen);

            addChild(layer1);
            addChild(layer2);
            addChild(layer3);
            addChild(layer4);
            addChild(layer5);
        }

        public function addGameChild(object:IGameObject, layer:Number):void
        {
            switch(layer)
            {
                case 1:
                    layer1.addChild(DisplayObject(object));
                break;
                case 2:
                    layer2.addChild(DisplayObject(object));
                break;
                case 3:
                    layer3.addChild(DisplayObject(object));
                break;
                case 4:
                    layer4.addChild(DisplayObject(object));
                break;
                case 5:
                    layer5.addChild(DisplayObject(object));
                break;
                default:
            }

            if (object.IsDisplay == true)
                gameObjects.push(object);

            stageObject.push(object);
        }

        public function UpDateMap():void
        {
            offsX += scrollx;
            offsY += scrolly;

            tilex = int(offsX/tileW);
            tiley = int(offsY/tileH);

            xEnd = tilex + vWidth;
            yEnd = tiley + vHeight;

            var tileNum:int;

            var tilePoint:Point = new Point(0,0);
            var tileRect:Rectangle = new Rectangle(0, 0, tileW, tileH);

            var rowCtr:int=0;
            var colCtr:int=0;

            for (rowCtr=0; rowCtr <= vRows; rowCtr++) {
                for (colCtr = 0; colCtr <= vCols; colCtr++) {

                    currentCol = colCtr+tilex;
                    currentRow = rowCtr+tiley;
                    tileNum = mapHolder[currentMap][rowCtr+tiley][colCtr+tilex];
                    tilePoint.x = colCtr * tileW;
                    tilePoint.y = rowCtr * tileH;
                    tileRect.x = int((tileNum % 100))* tileW;
                    tileRect.y = int((tileNum / 100))* tileH;
                    buffer.copyPixels(images[currentTileSheet],tileRect,tilePoint);
                }               
            }//End Loop
            var bgRect:Rectangle = new Rectangle(0, 0, 544, 510);
            var bgPoint:Point = new Point(0, 0);

            var bufferRect:Rectangle = new Rectangle(0,0,vWidth,vHeight);
            var bufferPoint:Point = new Point();            

            bufferRect.x = offsX % tileW;
            bufferRect.y = offsY % tileH;

            canvas.copyPixels(background,bgRect,bgPoint);
            canvas.copyPixels(buffer,bufferRect,bufferPoint);

        }//End UpdateMap

        public function StartRender():void
        {
            addEventListener(Event.ENTER_FRAME, loop);
        }

        protected function loop(e:Event):void
        {           
            UpDateMap();
            UpdateObjects();
        }

        protected function UpdateObjects():void
        {
            for (var i:Number = 0; i < stageObject.length; i++)
            {
                stageObject[i].UpdateObject();
            }

            for (var g:Number = 0; g < stageObject.length; g++)
            {
                if (stageObject[g].Garbage && stageObject[g].IsDisplay)
                {
                    removeChild(DisplayObject(stageObject[g]));
                    stageObject[g] = null;
                }
                else if (stageObject[g].Garbage == true && stageObject[g].IsDisplay == false)
                {
                    stageObject[g] = null;                  
                }
            }

        }

        public function StopRender():void
        {
            removeEventListener(Event.ENTER_FRAME, loop);
        }

    }

}

这不是完整的代码,但如果我删除canvas.copyPixels(background,bgRect,bgPoint);canvas.copyPixels(buffer,bufferRect,bufferPoint);,我可以看到或者。如果我把它们都画了,那么我只能看到最后画的那个。我的图块图像是128 x 32. 0 - 3.阵列3是透明图像。我用过那个希望能透过图像看到背景的东西。我错了。

起初它是全黑背景,但后来我在缓冲变量上将透明构造函数更改为true。现在它显示白色背景(如舞台),但仍然没有背景图像。

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解该示例,但看起来问题是您正在使用copyPixels - 这应该用新值替换canvas bmp中的旧像素

尝试使用draw代替前景:

canvas.copyPixels(background, bgRect, bgPoint);
canvas.draw(buffer, transform, null, null, clipRect);

另外,我不确定你为什么要将背景图像绘制到与前景不同的矩形?那里可能有东西。

答案 1 :(得分:0)

我拿了一个单独的电影剪辑,并用它作为blitting瓷砖背后的视差。