ActionScript - 使用cacheAsBitmap移动scrollRect时出现问题

时间:2011-08-30 09:41:38

标签: actionscript-3 caching bitmap scrollrect

为了提高scrollRect的性能,我必须将矢量缓存为位图,否则scrollRect将只是一个性能较低的掩码(info source)。

然而,一旦我应用了cacheAsBitmap,我似乎无法移动对象/ scrollRect。为什么呢?

package
{
    //Imports
    import flash.display.Screen;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;

    //Class
    [SWF(width = "800", height = "500", backgroundColor = "0x444444")]
    public class ScrollRectTest extends Sprite
    {
        //Variables
        private var background:Sprite;
        private var ball:Sprite;
        private var newScrollRect:Rectangle;

        //Constructor
        public function ScrollRectTest()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;

            init();
        }

        //Initialize
        private function init():void
        {
            background = new Sprite();
            background.graphics.beginFill(0x000000, 1.0);
            background.graphics.drawRect(0, 0, 200, 400);
            background.graphics.endFill();

            ball = new Sprite();
            ball.graphics.beginFill(0xFF0000, 1.0);
            ball.graphics.drawCircle(0, 0, 100);
            ball.graphics.endFill();
            //ball.cacheAsBitmap = true; //<-- uncomment this
            ball.scrollRect = new Rectangle(background.x, background.y, background.width, background.height);

            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);

            addChild(background);
            addChild(ball);
        }

        //Mouse Down Event Handler
        private function mouseDownEventHandler(evt:MouseEvent):void
        {   
            addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        //Mouse Up Event Handler
        private function mouseUpEventHandler(evt:MouseEvent):void
        {
            removeEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        //Enter Frame Event Handler
        private function enterFrameEventHandler(evt:Event):void
        {
            newScrollRect = ball.scrollRect;
            newScrollRect.y -= 10;
            newScrollRect.x -= 5;
            ball.scrollRect = newScrollRect;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

更有趣:

package
{
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;


    [SWF(width = "800", height = "500", backgroundColor = "0x444444")]
    public class ScrollRectTest extends Sprite
    {
        //Variables
        private var background:Sprite;
        private var ball:Sprite;
        private var newScrollRect:Rectangle;

        //Constructor
        public function ScrollRectTest()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;

            init();
        }

        //Initialize
        private function init():void
        {
            background = new Sprite();
            background.graphics.beginFill(0x000000, 1.0);
            background.graphics.drawRect(0, 0, 200, 400);
            background.graphics.endFill();

            ball = new Sprite();
            ball.graphics.beginFill(0xFFFF00, 1.0);
            ball.graphics.drawCircle(0, 0, 100);
            ball.graphics.endFill();

            var foo:Sprite = new Sprite();
            var g:Graphics = foo.graphics;
            g.beginFill(0xFF0000, 0.2);
            g.drawRect(0, 0, 100, 100);
            g.endFill();
            ball.addChild(foo);


            ball.cacheAsBitmap = true; //<-- uncomment this
            ball.scrollRect = new Rectangle(background.x, background.y, background.width, background.height);

            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);

            addChild(background);
            addChild(ball);
        }

        //Mouse Down Event Handler
        private function mouseDownEventHandler(evt:MouseEvent):void
        {   
            addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        //Mouse Up Event Handler
        private function mouseUpEventHandler(evt:MouseEvent):void
        {
            removeEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
        }

        //Enter Frame Event Handler
        private function enterFrameEventHandler(evt:Event):void
        {
            newScrollRect = ball.scrollRect;
            newScrollRect.y -= 1;
            newScrollRect.x -= 1;
            ball.scrollRect = newScrollRect;
//          ball.scaleX = 1 + Math.random() * 0.01;// uncomment this to force redraw
        }
    }
}

所以我认为这是一种错误。