bitmapdata.copyPixels会降低图像质量吗?

时间:2015-03-02 23:30:40

标签: actionscript-3 bitmap sprite-sheet

我正在使用一对类来处理和使用Sprite Sheets但我有一个小问题,与Bitmap类相比,我的SpriteSheetClips看起来非常难看,我使用完全相同的图像上下缩放Bitmap和SpriteSheetClip加载它们看起来非常不同Bitmap看起来很流畅,而我的SpriteSheetClips看起来像带有白色边框的像素化,我的类有什么问题导致了这个问题?

SpriteSheet(包含要用于剪辑的剪切位图数据):

    package src {

    import flash.display.BitmapData;
    import flash.geom.Rectangle;
    import flash.geom.Point;

    public class SpriteSheet {

        private var frames:Array = new Array();

        public function SpriteSheet(spriteSheet:BitmapData, spriteSheetXML:XML) 
        {
            var tileAmount:int = spriteSheetXML.SubTexture.length();
            var rect:Rectangle;
            var point:Point = new Point(0, 0);
            var frame:BitmapData;

            for (var a:int = 0; a < tileAmount ; a++)
            {
                frame = new BitmapData(spriteSheetXML.SubTexture[a].@width, spriteSheetXML.SubTexture[a].@height);
                rect = new Rectangle(spriteSheetXML.SubTexture[a].@x, spriteSheetXML.SubTexture[a].@y, spriteSheetXML.SubTexture[a].@width, spriteSheetXML.SubTexture[a].@height);
                frame.copyPixels(spriteSheet, rect, point);
                frames.push(frame);
            }
        }

        public function getTile(tileNum:int):BitmapData
        {
            return frames[tileNum-1];
        }

        public function get length():int
        {
            return frames.length;
        }

    }

}

SpriteSheetClip:

    package src {

    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.events.Event;

    public class SpriteSheetClip extends Sprite{

        private var spriteSheet:SpriteSheet;
        public var frame:Bitmap;
        private var _currentFrame:int = 1;
        private var _isPlaying:Boolean = true;
        public var isLooping:Boolean = true;

        private var frames:Array = new Array();

        public function SpriteSheetClip(spriteSheetUsed:SpriteSheet) 
        {
            spriteSheet = spriteSheetUsed;
            frame = new Bitmap(spriteSheet.getTile(1), "auto", true);
            this.addChild(frame);

            for (var a:int = 0; a < totalFrames; a++)
            {
                frames.push({labelName:"", action:""});
            }

            this.addEventListener(Event.ENTER_FRAME, onFrameLoop, false, 0, true);
        }

        public function get currentFrame():int
        {
            return _currentFrame;
        }

        public function get totalFrames():int
        {
            return spriteSheet.length;
        }

        public function get isPlaying():Boolean
        {
            return _isPlaying;
        }

        public function start()
        {
            _isPlaying = true;
        }

        public function stop()
        {
            _isPlaying = false;
        }

        public function addFrameLabel(frameNum:int, labelName:String)
        {
            if (frameNum == 0)
            {
                return;
            }
            frames[frameNum-1].frameLabel = labelName;
        }

        public function addConnection(frameLabel:*, connectedFrameLabel:*)
        {
            var frameNum:int = frameNumber(frameLabel);

            if (frameNum == 0)
            {
                return;
            }
            frames[frameNum-1].action = connectedFrameLabel;
        }

        private function frameNumber(frameLabel:*):int
        {
            if (int(frameLabel) <= totalFrames && int(frameLabel) > 0)
            {
                return int(frameLabel);
            }
            for (var a:int = 0; a < frames.length; a++)
            {
                if (frames[a].frameLabel == frameLabel)
                {
                    return (a+1);
                }
            }

            return 0;
        }

        public function gotoAndStop(frameLabel:*)
        {
            var frameNum:int = frameNumber(frameLabel);

            if (frameNum > 0 && frameNum <= totalFrames)
            {
                _currentFrame = frameNum;
            }
            updateFrame();
            stop();
        }

        public function gotoAndPlay(frameLabel:*)
        {
            var frameNum:int = frameNumber(frameLabel);

            if (frameNum > 0 && frameNum <= totalFrames)
            {
                _currentFrame = frameNum;
            }
            updateFrame();
            start();
        }

        private function updateFrame()
        {
            frame.bitmapData = spriteSheet.getTile(currentFrame);
        }

        public function nextFrame()
        {
            frame.bitmapData = spriteSheet.getTile(currentFrame);

            if (frames[currentFrame-1].action != "")
            {
                this.gotoAndPlay(frames[currentFrame-1].action);
                return;
            }

            _currentFrame++;
            if (currentFrame > totalFrames)
            {
                if (isLooping)
                {
                    _currentFrame = 1;
                }
                else
                {
                    _currentFrame = totalFrames;
                    _isPlaying = false;
                }
            }
        }

        private function onFrameLoop(e:Event)
        {
            if (isPlaying)
            {
                nextFrame();
            }
        }

    }

}

0 个答案:

没有答案